2

I am getting arraylist.get(i) every time a loop executes more than three times within the loop.

Is it advisable or shall I store it in separate variable then use it again and again? Which one is preferable performance wise?

Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
Siva Sankar
  • 387
  • 1
  • 2
  • 9
  • 2
    i is a condition variable, just a number. – Siva Sankar May 09 '16 at 15:48
  • `Get` it once and then use it throughout. – robotlos May 09 '16 at 15:49
  • I'd say you're not losing much, and the performance boost is negligible. Looking at the source [ArrayList](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#ArrayList.elementData%28int%29) , all it does is a range check to see if `i` is within the bounds of size, and returns the element from an array. – Clark Kent May 09 '16 at 15:52

4 Answers4

4

Setting it to a variable is slightly more efficient. Accesing arrayList.get (I) is O (1) but still costs something eventhough it is really minor and insignificant. Setting it to a variable is more readable in my opinion.

aviad
  • 1,553
  • 12
  • 15
4

It's always a good approach to write readable and maintainable code. Since you question is very broad so expect broad answers as well.

        List<Integer> integerList = new ArrayList<>();
        for (int i=0;i<integerList.size();i++) {
            Integer integerValue = integerList.get(i);
            // make sure integerValue is not null. 
            // Thanks @Tom for pointing this out 

            System.out.println (integerValue);

            // Do operations

            System.out.println (integerValue);

            // Do more operations

            System.out.println (integerValue);
        }

Now this is one time assignment but you can use it at multiple times. Now, for instance, you have to change the logic of program so that you want to get always i+1, it will be easy for you to change only once, not multiple times.

java8.being
  • 464
  • 3
  • 11
  • 1
    Just a note: `int integerValue = integerList.get(i);` is vulnerable to a `NullPointerException`. – Tom May 09 '16 at 15:56
  • @Tom Thank you very much, updated. – java8.being May 09 '16 at 15:58
  • @Tom How could it be vulnerable to the NPE? Wouldn't the condition of the loop not be met, causing it not to enter? – Clark Kent May 09 '16 at 15:59
  • 1
    @SaviourSelf since `Integer` is a wrapper class so, `Integer integer = null;` is possible. – java8.being May 09 '16 at 16:00
  • @SaviourSelf The list stores `Integer`, so every index can contain `null`. Calling `get` on such index will cause an unboxing (because the variable `integerValue` is of type `int` and not `Integer`) and unboxing `null` causes that exception. – Tom May 09 '16 at 16:01
  • 1
    @java8.being Well, I expected a change from `int integerValue` to `Integer integerValue`, but that comment is also ok :D. – Tom May 09 '16 at 16:03
3

As others mentioned, getting object one time is slightly more efficient. Of course most of times this won't produce any problems and you can't notice any differences.
Logically because it's an O(1) operation, it shouldn't cause any differences at all, but because it calls a function of an object of type ArrayList , It's less cache friendly and direct memory reference maybe needed. Still the difference is very little.

Alireza Mohamadi
  • 511
  • 5
  • 19
2

declaring and assigning a variable once like String myString = arraylist.get(i); will be marginally faster than calling arraylist.get(i) multiple times.

Once you've done this you can call any methods on the myString instance.

I assume that arraylist is of type ArrayList<String>.

you may want to include a null check in your loop as well:

for(int i = 0; i < arraylist.size(); i++){
    String myString = arraylist.get(i);
    if(myString != null){
        //any calls to methods on myString 
    }
}
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36