Results 1 to 9 of 9
  1. #1
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default 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

    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]);
    		}
    	}
    any help please?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by felito View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    i did
    Java 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;
    	}
    
    }
    and

    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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by felito View Post
    but the result is: Contents of ll: [Lcard.Card;@53ebd75b
    You're trying to print an array and that's the way arrays show their String representation. Use the Arrays.toString(yourArray) method for that purpose.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    JosAH i really appreciate your help, one last question

    Java 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]);
    		}
    my doubt is, how i can do the same with LinkedList, i need a cycle to create a list of cards

    thanks again
    Last edited by felito; 02-02-2011 at 04:38 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by felito View Post
    JosAH i really appreciate your help, one last question

    Java 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]);
    		}
    my doubt is, how i can do the same with LinkedList, i need a cycle to create a list of cards

    thanks again
    Create a List<Card> and .add(new Card( ... )) every card to it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by felito View Post
    52 add() ????????, one by one?

    is the unique alternative??, is not possible add elements to list with a cycle?
    Yep, you have to add your Cards one by one to the list but of course you can do that in a loop (if that is what you mean by 'cycle'):

    Java Code:
    for (int count = 0; count < 52; count++) { 
       yourList.add(new Card( ... ));
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Biginner! Array to LinkedList
    By SungsooKim in forum New To Java
    Replies: 3
    Last Post: 09-28-2009, 08:18 AM
  2. help with x:transform
    By vicedja in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-10-2009, 07:42 PM
  3. Transform Scale
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:53 PM
  4. Transform Shear
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:52 PM
  5. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •