itzFlubby
itzFlubby

Reputation: 2289

Is it better to make temporary copies of elements in an ArrayLists to reduce computing time and complexity?

I've got an ArrayList consiting of objects card, which have the attributes (int) value, (String) symbol and (String) image and to them belonging getters.

ArrayList<card> cardDeck = new ArrayList<>();

cardDeck has self-explanatory 52 elements, each a card object.
Now, if I want to print out all the cards in cardDeck there are two simple solutions:


First solution:

for(int i = 0; i < cardDeck.size(); i++){
    System.out.println((i + 1) + ". card:");
    System.out.println(cardDeck.get(i).getValue());
    System.out.println(cardDeck.get(i).getSymbol());
    System.out.println(cardDeck.get(i).getImage());
}

Second solution:

for(int i = 0; i < cardDeck.size(); i++){
    card temp = cardDeck.get(i);
    System.out.println((i + 1) + ". card:");
    System.out.println(temp.getValue());
    System.out.println(temp.getSymbol());
    System.out.println(temp.getImage());
}

My question is, if there are any noticeable difference in either execution time or complexity.
On the first thought, in the first solution the program would have to look up the card in the ArrayList first every time, before being able to print its info. Which isn't the case in the second solution, as a temporary copy was made.
On second thought though, even in the second solution, the program would still need to look up the info of the temporary card object with every call.
Any help / ideas / advice appreciated!

Upvotes: 1

Views: 52

Answers (4)

khituras
khituras

Reputation: 1091

Peter has already said what would be the better idea from the programming perspective. I want to add that the OP asked about complexity. I interpret that in the sense of asymptotical time required in relation to the size of the card deck.

The answer is that from a theoretical complexity perspective, both approaches are the same. Each array lookup adds a constant factor to required time. It's both O(n) with n being the number of cards.

On another note, the OP asked about copying elements of the list. Just to make it clear: The statement card temp = cardDeck.get(i) does not cause the ith list element to be copied. The temp variable now just points to the element that is located at the ith position of cardDeck at the time of running the loop.

Upvotes: 1

hamidreza75
hamidreza75

Reputation: 707

First, you have other solutions for example, using for eatch loop or using forEatch method with lambda expressions. And about speed, you don't have to worry about speed until your program runs in regular computers and you don't have to deal with weak or low processors, but in your case, you can make your app less complex with using functional programming e.g,

cardDec.forEatch(card) -> {
    System.out.println(card.getValue());
    System.out.println(card.getSymbol());
    System.out.println(card.getImage());

};

Upvotes: 0

Peter Walser
Peter Walser

Reputation: 15706

So we have

  • 3 array lookups (with the same index and no modification on the array, so the compiler MAY optimize it) in the first solution
  • error prone code in the first solution (what happens if you need to change the index to i+1 and forget to correct the code in all 3 places)

versus:

  • 1 array lookup in the second solution - optimized without relying on the compiler
  • better readable code in the second solution (if you replace temp by card, which you can do if you properly start the class name uppercase: Card card)

Array lookups are not that cheap in Java - the arrays are guarded (bounds checks) to prevent buffer overflow injection vulnerabilities.

So you have two very good reasons that tell you to go with the second solution.

Upvotes: 2

Nandu Raj
Nandu Raj

Reputation: 2110

Using Java 8,

    cardDeck.parallelStream().forEach(card -> {System.out.println(card.getValue());System.out.println(card.getSymbol());System.out.println(card.getImage());});

This does not guarantee better performance, this depends on the number of CPU cores available.

Upvotes: 1

Related Questions