Q:

Create a list to store cities called cityList. Add the following cities to cityList:

0

Create a list to store cities called cityList.

Add the following cities to cityList: London, Paris, Denver, Miami, Tokyo, Seoul.

Print out the list contents.

Print the size of the cityList.

Print out if Miami is in cityList.

Print out the location of Denver in the list.

Add another city Paris to the list in location 4.

Remove the first occurrence of “Paris” from the list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.ArrayList;
public class Main
{
	public static void main(String[] args) {
	// 1
ArrayList<String> cityList = new ArrayList<>();
// 2
cityList.add("London");
cityList.add("Denver");
cityList.add("Paris");
cityList.add("Miami");
cityList.add("Seoul");
cityList.add("Tokyo");
//3
System.out.println(cityList);
//4-6
System.out.println("List size? " + cityList.size()); 
System.out.println("Is Miami in the list? "+ cityList.contains("Miami")); 
System.out.println("The location of Denver in the list? "+  cityList.indexOf("Denver")); 
//7-8
cityList.add(4, "Paris");
cityList.remove ("Paris");


//Printing the ArrayList Object
//There are 3 ways to print the ArrayList object:
//way 1:
System.out.println(cityList);

//way 2:
//Using for loop as in the array:
	    for(int i=0; i<cityList.size(); i++)
            System.out.println(cityList.get(i));

//way3:
//For each String X belong to cityList, x will hold the value itself not the index.
//The print statement could be replaced by other statement.
for(String x : cityList)
     System.out.println(x);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now