Created
February 14, 2012 22:35
-
-
Save NickCarneiro/1831120 to your computer and use it in GitHub Desktop.
Demonstration of ArrayList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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