1 / 3

ArrayList Review Solutions

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”);

Download Presentation

ArrayList Review Solutions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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);

  2. 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();

  3. import java.util.ArrayList;

More Related