Results 1 to 6 of 6
Thread: Strange for loop
- 01-21-2011, 11:43 AM #1
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
Strange for loop
I use this bottom code for my project, but I have a question about the for(Car car: cars) loop. I've been trying too find an explanation for this for loop but haven't got any results so far. Why does the for loop first create an object? I assume the arraylist cars in this for loops just sets the end of the for loop.
I've got another small question which has nothing too do with the above code.Java Code:ArrayList<Car> cars = Main.getQeuries().getCar(); DefaultTableModel model = (DefaultTableModel) tblResults.getModel(); for (Car car : cars) { ranking++; model.addRow(new Object[]{ ranking, boek.brand, boek.maxSpeed, boek.seets}); }
When i set the following code in my main class it does not create an object or calls the constructor.
What exactly does new Car(5,5,5,5); do?Java Code:public static void main(String[] args) { new Car(5,5,5,5); }
greets,
Grimmjow
- 01-21-2011, 11:48 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In for loop, you want to a row as each number of items that contain in Cars ArrayList.
- 01-21-2011, 01:09 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
It does both.
It creates a Car object using the constructor that takes those parameters.
Then the program exits.
This prints out "In constructor".Java Code:public class Blah { private static class Car { Car(int a, int b, int c) { System.out.println("In constructor"); } } public static void main(String args[]){ new Car(5,5,5); } }
- 01-23-2011, 01:24 PM #4
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
But imagine that I want to use the car class again. I can't use the object created by new Car(4,5,4) right? And is it allright to just call new Car(5,5,5) in any case?
p.s. i still dont really understand what the for(Car car: cars) does
- 01-23-2011, 10:04 PM #5
No you cannot "use" the car as you have thrown away the reference to it. Remember that the new operator creates an object by calling the constructor and then returning a reference to that object. Therefore you need to store it somewhere.
Variable: Foo f = new Foo();
Array: fooArray[index] = new Foo();
List: fooList.add(new Foo());
etc
Perfectly alright. It compiles, it runs but as you have ascertained not very useful in certain situations.And is it allright to just call new Car(5,5,5) in any case?
It is called the enhanced for loop. Alternatively called the for each loop. Basically it is saying "for each object in the list....". So if you have a List or an array or some other iterable data structure the loop will execute N times where N is the number of objects in the data structure.p.s. i still dont really understand what the for(Car car: cars) does
- 01-23-2011, 10:52 PM #6
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
calling Graphics functions within for loop yields strange results
By lmtoe in forum AWT / SwingReplies: 7Last Post: 01-17-2011, 07:02 AM -
Explanation of Nested Loop (very strange)
By Jonotron in forum New To JavaReplies: 5Last Post: 01-09-2011, 02:54 AM -
strange problem
By dinosoep in forum New To JavaReplies: 6Last Post: 05-01-2010, 10:28 AM -
Strange Problem
By Aseem in forum JDBCReplies: 5Last Post: 02-12-2010, 07:46 AM -
Very strange structure
By atch in forum New To JavaReplies: 1Last Post: 02-10-2010, 11:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks