Skip to content

Instantly share code, notes, and snippets.

@memish
Last active October 16, 2017 23:43
Show Gist options
  • Select an option

  • Save memish/5953ebba156af6ec243285865ee2931d to your computer and use it in GitHub Desktop.

Select an option

Save memish/5953ebba156af6ec243285865ee2931d to your computer and use it in GitHub Desktop.
ArrayLists Example
import java.util.*;
public class ArrListExample {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
ArrayList<String> cityPop = new ArrayList<String>();
cityPop.add("Tokyo, Japan - 28,025,000");//ADD METHOD
cityPop.add("Philadelpia, USA - 8,131,000");
cityPop.add("Mexico City, Mexico - 8,131,000");
cityPop.add("Mumbai, India - 1,042,000");
cityPop.add("New York City, USA - 16,626,000");
cityPop.remove(1);//removes that elements and shifts rest down
cityPop.add(3,"Sáo Paulo, Brazil - 7, 711,000");
//ANOTHER ADD METHOD - places in 3rd slot and slides rest down
/*
* NOTE: if you run this program as is, nothing will show to the screen
* Use the for loop below to print the cityPop array to the screen
See if the order is what you expected.
Also, use the remove and set methods to add and remove 
elements from the ArrayList. See if it works as intended.
*/
for (int i=0; i<cityPop.size();i++){//SIZE METHOD 
// FILL IN BODY
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment