2

For example lets say I've create a single dimensional array and initiate the array to 100. If I want to be able to use all the values of the array, like in a for loop, how would I do it? I found two methods below but I'm not sure which approach is recommended. Also is there any differences at all?

int[] list = new int[100];
for (int i = 0; i < list.length; i++) 

or

for (int i = 0; i < 100; i++) 

Which of these two syntax is more commonly used?

takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • The first is better. On a side note you should define and use a constant with an informative name, instead of declaring a new array with a *magical number* ie: `final int LIST_SIZE = 100;` and then `int[] list = new int[LIST_SIZE];`. – Jonny Henly Jan 06 '15 at 04:49

5 Answers5

10

This is much better:

for (int i = 0; i < list.length; i++)

If you change your mind, and decide that the list needs to be of length 150 instead, then the rest of your code will still work.

The other option requires you to change all of your for loops if you change the length of the array at all.

There aren't really any other differences.

EDIT: As Manoj Sharma mentioned in his answer, there's another thing you can do:

for (int myInt : list)

This will do the iteration for you, but you can't get the index in the loop.

I'm mentioning this for completeness, but if you found this helpful, upvote his answer!

Community
  • 1
  • 1
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • 3
    list.length for an array, `.size()` is a property of the `List` object. See http://stackoverflow.com/questions/9297899/arrays-length-property – Anubian Noob Jan 06 '15 at 04:45
  • just read list without seeing it actually was an array. silly me :) – holtc Jan 06 '15 at 04:46
1

for (int i = 0; i < list.length; i++) It is dynamic condition handling, It will change according to your array length.

for (int i = 0; i < 100; i++) It is static condition handling, If you have thousands of for loop you need to change each loop condition.

Evey time make sure that you avoid hard coding in your application.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
1

Well above answer given by @Anubian Noob is good. but java5 onward java provides enhanced for each loop which is far better than other. Even no error chance in this method. look below:

for(int i: list){
System.out.println(i);
}

this will loop through the entire array and display all the element store in an array. you can also use this method for multi dimensional array too.

Manoj Sharma
  • 596
  • 1
  • 6
  • 23
  • 1
    But the downside is you can't get the index in your loop, which can be annoying if you need it. Useful to mention though. – Anubian Noob Jan 06 '15 at 04:51
  • It depends on condition, we have while and do while loop too. in some situation while loop is better then for loop. same if you don't want index number you just want fastest and effective without error prove structure then then go for enhanced for loop instead simple for loop. – Manoj Sharma Jan 06 '15 at 04:53
  • Yeah, it depends on your application. This is way cleaner and easier to use, but has some tradeoffs. – Anubian Noob Jan 06 '15 at 04:59
0

It is better to store the length of the array in a variable and then use it. Instead of hard coding it in the loop or find it every time loop runs.

var arraySize = yourArray.length;

for(i;i<arraySize; i++){
//your code here.
}
Superman
  • 871
  • 2
  • 13
  • 31
  • Why is it better? From my experience when I've tried benchmarking it it's barely faster. Barely as in 1% on average. But I haven't done too much. – Anubian Noob Jul 06 '15 at 18:19
0

In Java 8 you are much better off learning to use streams. They are more intuitive and you don't need to worry about anything like the size.

For example:

int list[] = {4, 7, 3, 9, 11};
Arrays.stream(list)...

In this case Arrays.stream knows that you have created an IntStream so you get max, min, sorted, sum etc.

You need for loop much less frequently now.

sprinter
  • 27,148
  • 6
  • 47
  • 78