Results 1 to 3 of 3
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Problem with LinkedList Java

    I have 2 lists linkedlist and linkedlist2.
    I want to be able to take out the first Card out of linkedlist (which starts at 0) and add it to linkedlist2. Then once that is done i want to remove Card 0 from linkedlist, and i want to be able to do this for as many times as i assign to the integer n.

    however the part that i bolded doesn't work so how do i do it?

    Java Code:
    public Deck split(int n) {
            //implement
            LinkedList<Card> linkedlist= new LinkedList<Card>();
            LinkedList<Card> linkedlist2= new LinkedList<Card>();
            
            int i = n;
            
            while (i > 0){
            linkedlist2.add(linklist<0>);
            linkedlist.remove(0);
            
            i--;}
    Thanks

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Which bold part?

  3. #3
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    Gox is right:
    Java Code:
    public Deck split(int n) {
            //implement
            LinkedList<Card> linkedlist= new LinkedList<Card>();
            LinkedList<Card> linkedlist2= new LinkedList<Card>();
            
            int i = n;
            
            while (i > 0){
            linkedlist2.add(linkedlist.get(0));
            linkedlist.remove(0);
            
            i--;
    }
    You could also change it to

    Java Code:
    linkedlist2.add(linkedlist.poll());
    There are a whole bunch of different ways to do it so I suggest looking at the API

Similar Threads

  1. Making a stack from a LinkedList
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:28 PM
  2. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:24 PM
  3. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM
  4. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 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
  •