Skip to content

Instantly share code, notes, and snippets.

@NickCarneiro
Created February 14, 2012 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickCarneiro/1831120 to your computer and use it in GitHub Desktop.
Save NickCarneiro/1831120 to your computer and use it in GitHub Desktop.
Demonstration of ArrayList
import java.io.*;
import java.util.ArrayList;
public class ArrayListDemo {
//program for storing an arbitrary number of strings in a list and then printing them out.
public static void main(String[] args){
ArrayList<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a list of your favorite foods.");
System.out.println("Type \"done\" when finished.");
//infinite loop breaks when user types "done"
while(true){
try{
String line;
line = reader.readLine();
if(line.equals("done")){
break;
} else {
//if user didn't type exit, add the input to our arraylist
list.add(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("");
System.out.println("Your favorite foods are:");
// print out everything in the list
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment