Results 1 to 9 of 9
Thread: transform array in linkedlist
- 02-02-2011, 03:25 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
transform array in linkedlist
hi guys
i want to convert my code, that use an array to a linked list, the reason is that is more flexible and fast for future implementation's of my script
but i can't convert this code
any help please?Java Code:public Deck() { int number[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 2, 4, 11 }; ImageIcon[] Image = { new ImageIcon("../KingGame/src/game/img/1.gif"), new ImageIcon("../KingGame/src/game/img/2.gif"), new ImageIcon("../KingGame/src/game/img/3.gif"), //more ImageIcon }; deckOfCards = new Card[number_cards]; for (int count = 0; count < deckOfCards.length; count++) { deckOfCards[count] = new Card(Rank.values()[count % 13].toString(),Suit.values()[count / 13].toString(), number[count % 13],Image[count % 52]); } }
- 02-02-2011, 03:43 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
What can't you convert? What exactly is the problem? To place the values of an array to a List, just iterate over the values and add them to the List.
- 02-02-2011, 03:44 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
The Arrays class has a method that can convert arrays to Lists; but it can only do that if the array contains Objects (of any type), not for arrays that contain primitives (ints, doubles etc.) For an int array you have to feed all the elements of the array to a List.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2011, 03:50 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
i did
andJava Code:package card; import java.util.Collections; import java.util.LinkedList; import javax.swing.ImageIcon; public class Teste { private Card deckOfCards[]; private final int number_cards = 52; public ImageIcon cardImage; public Teste() { int number[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 2, 4, 11 }; ImageIcon[] Image = { new ImageIcon("../KingGame/src/game/img/1.gif"), new ImageIcon("../KingGame/src/game/img/2.gif"), new ImageIcon("../KingGame/src/game/img/3.gif"), new ImageIcon("../KingGame/src/game/img/4.gif"), //more }; setDeckOfCards(new Card[number_cards]); LinkedList<Card> deckOfCards = new LinkedList<Card>(); for (int count = 0; count < deckOfCards.size(); count++) { deckOfCards.add(new Card(Rank.values()[count % 13].toString(),Suit.values()[count / 13].toString(), number[count % 13],Image[count % 52])); } Collections.shuffle(deckOfCards); } public void setDeckOfCards(Card deckOfCards[]) { this.deckOfCards = deckOfCards; } public Card[] getDeckOfCards() { return deckOfCards; } }
Java Code:package card; public class Final { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final Teste gr1 = new Teste(); System.out.println("Contents of ll " + gr1.getDeckOfCards()); } }
but the result is: Contents of ll: [Lcard.Card;@53ebd75b
- 02-02-2011, 03:58 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 02-02-2011, 04:13 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
JosAH i really appreciate your help, one last question
my doubt is, how i can do the same with LinkedList, i need a cycle to create a list of cardsJava Code:for (int count = 0; count < deckOfCards.length; count++) { deckOfCards[count] = new Card(Rank.values()[count % 13].toString(),Suit.values()[count / 13].toString(), number[count % 13],Image[count % 52]); }
thanks againLast edited by felito; 02-02-2011 at 04:38 PM.
- 02-02-2011, 04:52 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 02-02-2011, 04:57 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
52 add() ????????, one by one?
is the unique alternative??, is not possible add elements to list with a cycle?Last edited by felito; 02-02-2011 at 05:01 PM.
- 02-02-2011, 05:07 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Biginner! Array to LinkedList
By SungsooKim in forum New To JavaReplies: 3Last Post: 09-28-2009, 08:18 AM -
help with x:transform
By vicedja in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-10-2009, 07:42 PM -
Transform Scale
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:53 PM -
Transform Shear
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:52 PM -
Creating Array of LinkedList
By sasikumardr in forum New To JavaReplies: 1Last Post: 12-11-2007, 10:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks