3

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) 
}
1
  • Uh, what? @Jayson He didn't ask for two-dimensionals... Commented Nov 12, 2012 at 22:47

6 Answers 6

2
List<Temperature> temperatures = new ArrayList<Temperature>();
for (int i = 0; i < 3; ++i) {
    Temperature t = new Temperature();
    t.setDegrees(user input) ;
    temperatures.add(t);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

But I still cannot access newly created instance of Temperature outside the loop since Temperature is only created inside the for loop. the scope of for loop ends within the braces. I want to be able to set some parameter for the instance like t.setDegrees inside of a class but access that parameter outside by t.getDegrees
@SudeshBanskota: You can access all of them from the ArrayList - unlike C++, objects are always created dynamically ("On heap") - and if you have reference to them - you can access them. In this case - you have a reference to the Temprature objects in your ArrayList, and you can get them by using list.get(1),list.get(2),... or by iterating the ArrayList.
1
    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.

Comments

1
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

Comments

0
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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.