Purchase Solution

Java Arraylist

Not what you're looking for?

Ask Custom Question

I am still sorting through this chapter on arrays and ran into another exercise that I do not understand. If someone could please help a struggling single mom figure this out it would be much appriciated. This program is in Java.

The following code creates a simple ArrayList:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Ex01 {
public static void main(String[] args) throws IOException {
BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("Zero");
myArr.add("One");
myArr.add("Two");
myArr.add("Three");
}
}
Starting with this provided code, add the following functionality:
1. Add items to the ArrayList, one at a time, based on user typed input. The user will be prompted for the String to be stored in the ArrayList and then hit enter. The user will be able to continue to add items to the ArrayList until they just hit enter without typing anything.
2. Once the user does this (hits enter without typing anything), the program will display all of the elements of the ArrayList, both the index and String values, in a table. It will do this via a single loop.

Purchase this Solution

Solution Summary

This solution provides a detailed example of using and ArrayList with loops in Java. It also shows how to read user input from the keyboard.

Solution Preview

Solving this problem requires adding two loops to the existing
program. The first loop is used to read user input and store the
values in the ArrayList object.

<pre>
boolean done = false;
while (!done) {
System.out.print("Enter a string value: ");
String value = userInput.readLine();
if (value.equals("")) ...

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

C# variables and classes

This quiz contains questions about C# classes and variables.