30 likes | 128 Views
ArrayList Review Solutions. How would you create an array list to store the cities in North Carolina? ArrayList<String> cities = new ArrayList<String>(); How would you put “Greensboro” and “Raleigh” into this array list? cities.add(“Greensboro”); cities.add(“Raleigh”);
E N D
ArrayList Review Solutions How would you create an array list to store the cities in North Carolina?ArrayList<String> cities = new ArrayList<String>(); How would you put “Greensboro” and “Raleigh” into this array list?cities.add(“Greensboro”); cities.add(“Raleigh”); Suppose the array list is full of cities, but the last city inserted is a duplicate and needs to be removed. How would you remove the last city?cities.remove(cities.size()-1);
ArrayList Review Solutions “Kernersville” changed its name to “K-Vegas”. Find this element and change it.for (int i=0; i<cities.size(); i++){ if (cities.get(i).equals(“Kernersville”) { cities.set( i,”K-Vegas”); break; }} You need to start over and get rid of all the cities in the array list. How would you do that?cities.clear();