Results 1 to 6 of 6
  1. #1
    Grimmjow is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default 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.

    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});
            }
    I've got another small question which has nothing too do with the above code.

    When i set the following code in my main class it does not create an object or calls the constructor.

    Java Code:
        public static void main(String[] args) {
            new Car(5,5,5,5);
        }
    What exactly does new Car(5,5,5,5); do?

    greets,
    Grimmjow

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,958
    Rep Power
    19

    Default

    Quote Originally Posted by Grimmjow View Post
    When i set the following code in my main class it does not create an object or calls the constructor.
    It does both.

    It creates a Car object using the constructor that takes those parameters.

    Then the program exits.

    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);
        }
    }
    This prints out "In constructor".

  4. #4
    Grimmjow is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by Grimmjow View Post
    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?
    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

    And is it allright to just call new Car(5,5,5) in any case?
    Perfectly alright. It compiles, it runs but as you have ascertained not very useful in certain situations.

    p.s. i still dont really understand what the for(Car car: cars) does
    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.

  6. #6
    Grimmjow is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    Many thanks to all! You where a great help:D I will mark this thread as solved

    greets,
    Grimmjow

Similar Threads

  1. Replies: 7
    Last Post: 01-17-2011, 07:02 AM
  2. Explanation of Nested Loop (very strange)
    By Jonotron in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 02:54 AM
  3. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 10:28 AM
  4. Strange Problem
    By Aseem in forum JDBC
    Replies: 5
    Last Post: 02-12-2010, 07:46 AM
  5. Very strange structure
    By atch in forum New To Java
    Replies: 1
    Last Post: 02-10-2010, 11:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •