• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping through using for loop to add to Array:  RSS feed

 
Alex Walton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am new to this whole thing, including Java!
I have code, that basically allows me to do the bellow. However, I am trying to use the for loop to add each car (car1, car2...etc ) to the queue1.

I cannot see a way that I could use the int I (for example Car + i) to allow me to go through all the new cars that I would like to add. At the moment the code just repeatedly adds car1 to the queue.

is there something i am missing?

Thanks guys!


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Walton wrote:
is there something i am missing?


You are not using an array to store those car instances. You are using 10 different local variables to store those variables -- and there isn't an easy way to iterate through those variables.

Henry

PS... welcome to the ranch.
 
Alex Walton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Alex Walton wrote:
is there something i am missing?


You are not using an array to store those car instances. You are using 10 different local variables to store those variables -- and there isn't an easy way to iterate through those variables.

Henry

PS... welcome to the ranch.


Hi Henry,

So could i create an array in the Main programme, have the 10 cars in there, and then loop through? I am slightly stumped at an easy way to get the 10 cars into the queue1!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Walton wrote:
So could i create an array in the Main programme, have the 10 cars in there, and then loop through? I am slightly stumped at an easy way to get the 10 cars into the queue1!


Absolutely. Have you learned arrays yet?  If not, here is the Oracle tutorial on arrays...

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Henry
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that if you are going to add a Car to an array one by one, you should just add it to the queue one by one.

There is actually an easier way to do this if you don't need the carN variables.  Can you see how to do this?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Walton wrote:. . . So could i create an array in the Main programme, have the 10 cars in there, and then loop through? I am slightly stumped at an easy way to get the 10 cars into the queue1!
No. There shou‍ld be no such thing as a main program, only a class with a main method, which is different. The main method is only for starting the application, and everything else is done elsewhere.
I presume that when you said array in the thread title you meant array. Some people say array when they mean list, which is different. I would suggest you have a CarQueue class, think of a better name for it, and give it a Car[] array as a field. If any access to the cars is required outside the queue class, you can provide methods to allow such access.
There is an alternative to adding things to an array. You know that when you create an array of reference types, it is filled with nulls as a default, until you reassign them.I don't know what you have written for the toString method in the Car class, so I simply wrote car0, car1, etc.
If you can fill your array with an array initialiser, you can be sure of two things:-
  • 1: The array will be exactly the same size as the number of instances in it.
  • 2: There will only be nulls if you write null.
  • If you declare and initialise the array on the same line you are allowed to omit new Car[]

    And welcome again
     
    Campbell Ritchie
    Marshal
    Posts: 56598
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are not using your car queue object as an array, but as a very simple list. You can write an array as a field, and give it a size field. Now, adding to the array can look like this:-
    myArray[size++] = carArgument;
    I don't think I am cheating by showing you that code because you wouldn't guess it otherwise. If somebody tries to get a car from an index outside the bounds of the list (not the array) (i.e. index < 0 or index ≥ size), throw an Exception. If the array gets full, you have the option of throwing an Exception to say the array is full, or copying the array into a larger array.
    Removing things from the queue, well that's a different story requiring different code.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!