user1819438
user1819438

Reputation:

ArrayList in Java

I need to create 3 instances of my temperature class. I can do this by typing it three times, but I am sure that it is not a good way to do it. Hence, I searched online and found ArrayList.

I don't understand how to use ArrayList so it can create instances of class in a loop, and I want to access it outside my loop.

For example right now I have

Temperature t1 = new Temperature ();
t1.setDegrees(input.nextDouble());

How do i loop the whole thing above so i dont have to type it three times. I searched online for examples but all i can find is values that are already stored in an ArrayList, instead of new instances being created and being stored inside of the ArrayList. So basically this is what i want to do

Temperature t1 = new Temperature ()
for ( 3 loops ) 
{

t1.setDegrees(user input) ;
ArrayList ( << this is the part i need help on. How do i store the temperature here so i can access it outside the scope of the for loop) 
}

Upvotes: 3

Views: 1697

Answers (6)

Tyler
Tyler

Reputation: 314

Having an ArrayList or even an array of Temperatures is not going to be any better memory/speed-wise than just declaring 3 Temperatures.

And, depending on what you're trying to do, you're probably better off declaring them separately so that you can reference them by name.

Temperature fahrenheit = new Temperature();
Temperature celsius = new Temperature();

In this way you can access them by name, whereas if they were in an array you would have to remember which one was at which position in the array.

That said, this only works if you know in advance how many Temperatures you are going to need. If you don't know how many you will need, that is when you need to use an array or List.

Upvotes: 0

Pulkit Goyal
Pulkit Goyal

Reputation: 5664

List<Temperature> temperatures = new ArrayList<Temperature>();
for (int i = 0; i < 3; ++i) {
    Temperature t = new Temperature();
    t.setDegrees(user input) ;
    temperatures.add(t);
}

Upvotes: 2

thedayofcondor
thedayofcondor

Reputation: 3876

List<Temperature> temps= new ArrayList<Temperature>();

for (int i=0;i<3;i++){
    Temperature t = new Temperature ()
    t.setDegrees(user input) ;
    temps.add(t)
}
System.out.println(temps);

Upvotes: 0

Alex
Alex

Reputation: 25613

List<Temperature> temperatures = new ArrayList<Temperature>();
for (int i = 0; i < 3; ++i) {
    Temperature t = new Temperature();
    t.setDegrees(userInput);
    temperatures.add(t);
}
// here you go

Upvotes: 1

PermGenError
PermGenError

Reputation: 46428

    List<Temparature> templist = new ArrayList<>();
    for(int i=0; i<3; i++){
     Temparature t = new Temparature();
     t.setDegrees(input.nextDouble());
    tempList.add(t);
    }

the above code will create 3 Temparature objects in a loop and add them on every iteration.

check ArrayList API for its methods.

Upvotes: 1

amit
amit

Reputation: 178521

You are looking for ArrayList.add() method and a for loop:

List<Temprature> list = new ArrayList<Temprature>();
for (int i = 0; i < 3; i++) { 
   Temprature t = new Temprature();
   t.setDegrees(input.nextDouble());
   list.add(t);
}

To later get an element you can use ArrayList.get(), and if you need to iterate all objects you can use a for-each loop.

Upvotes: 1

Related Questions