Hi.
I have this code but I want it to store the same data and function using objects.
However, I would like to also know how to get information from the objects in an ArrayList.
Currently the information is stored directly without the using of objects.
I also want to know the special use of this ArrayList, in this particular scenario. I know its resizeable, flexible and a lot more text book things. However, I would love to know more about it and if it is possible for someone to help me understand concepts by modifying this code and showing me a variety of it , like as I said, objects in ArrayList , setting and getting data; maintain the same purpose of the program at the same time.

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("Italian Riviera");
        myArr.add("Jersey Shore");
        myArr.add("Puerto Rico");
        myArr.add("Los Cabos Corridor");
        myArr.add("Lubmin");
        myArr.add("Coney Island");
        myArr.add("Karlovy Vary");
        myArr.add("Bourbon-l'Archambault");
        myArr.add("Walt Disney World Resort");
        myArr.add("Barbados");
 
        System.out.println("Stupid Vacation Resort Adviser");
        System.out.println("Enter your name:");
        String name = userInput.readLine();
        Integer nameLength = name.length();
        if (nameLength == 0) 
        { 
            System.out.println("empty name entered");
            return;
        } 
 
        Integer vacationIndex = nameLength % myArr.size();
 
        System.out.println("\nYour name is "+name+", its length is " + 
                        nameLength + " characters,\n" +
                        "that's why we suggest you to go to " 
                        + myArr.get(vacationIndex));
    } 
}

> Currently the information is stored directly without the [use] of objects.
> ArrayList<String> myArr = new ArrayList<String>();
Not true. String is a class.

> I also want to know the special use of this ArrayList, in this particular scenario.
> Integer vacationIndex = nameLength % myArr.size();
ArrayList is a generic typed list, declared with a class name as its type and containing standard methods like get_at, remove_at, and insert. If you want anything specialized you'll probably end up making your own list class.

ArrayLists are used instead of arrays because it is dynamic meaning it can expand unlike arrays which are fixed sized. It is used when you do not know beforehand how many items you are going to put in your arrayList. Example would be if you are reading your vacation list from a text file, then you do not know how many places are there. In your example, since you are listing your vacation spots in your program use array.

My suggestion is to put your vacation spots in a text file then read the text file line by line adding every line in your arrayList. With this you can add vacation spots in your file without having to change your program.

try {
			BufferedReader inGen = new BufferedReader(new FileReader(filename));
			
			while ((String keyGen = inGen.readLine())!= null) {
								
				myArray.add(keyGen);
				
			}
		}
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace;
		}
		catch (IOException ioe){}
		finally {
			if (inGen != null) {
				try {
					inGen.close();
				}
				catch (IOException ioe) {}
			}
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.