Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (alprincenofl):

Which statement deletes the last element in an array list named players?

OpenStudy (alprincenofl):

Screenshot & choices: http://prntscr.com/5fuaxk

OpenStudy (harsha19111999):

Don't know. Sorry

OpenStudy (alprincenofl):

Don't be sorry, it's ok, I have to go now so no more questions today xD lol

OpenStudy (harsha19111999):

LOL. Okay. Sure

OpenStudy (woodrow73):

size() method returns the number of elements in an ArrayList. Each element of the ArrayList is assigned an index.. if you have 3 String objects in an ArrayList, 0, 1, and 2 are their indexes. Since size() on that ArrayList would return 3, to get the final element in an ArrayList we do size() then subtract one. remove() is the method of ArrayList that deletes an element, taking an int index as an argument (an argument is what you put inside the parenthesis) ie; ``` import java.util.ArrayList; public class Woodrow { public static void main(String[] args) { ArrayList<String> str = new ArrayList<>(); String name = "woodrow"; for(int i = 0; i < 3; i++) str.add(name); for(int i = 0; i < str.size(); i++) System.out.println(str.get(i)); //prints the whole arraylist of "woodrow" 3 times //doing str.get(3); probably will cause an error in the program str.remove(str.size()-1); //removes the final element for(int i = 0; i < str.size(); i++) System.out.println(Str.get(i)); //prints out whole arraylist of "woodrow" 2 times } } ```

OpenStudy (woodrow73):

the str in the final line of code is accidentally uppercase.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!