Reputation: 2289
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:
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
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
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
Reputation: 15706
So we have
versus:
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
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