Results 1 to 3 of 3
  1. #1
    Aiquoc is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Array to Linked List

    Need help, does anyone know how to:

    How do I Change this to LinkedLists

    private final int maxCrimes = 10;
    private Crime[] crime = new Crime[maxCrimes];
    private int crimeNo = 0;
    private Date start, end;
    private int highestCrimeLevel = 0;

    public Crimes() {
    crime[crimeNo] = new Crime("crime");
    start = crime[crimeNo].start();
    end = crime[crimeNo].end();
    crimeNo++;
    }

    I know how to change the array:
    from: private Crime[] crime = new Crime[maxCrimes];
    to : private LinkedList<Crime> crime = new LinkedList<Crime>();

    and adding elements to the list:
    from: crime[crimeNo] = new Crime("crime");
    to : crime.add(new Crime("crime"));

    but my problem is I don't know how to convert this to list

    start = crime[crimeNo].start();
    end = crime[crimeNo].end();

    I tried:
    start = crime.start();
    end = crime.end();

    Thanks :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    The Arrays class and the Collections framework can help you out:

    Java Code:
    T[] array= new T[ ... ];
    List<T> list= new LinkedList<T>(Arrays.asList(array));
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by jackycui View Post
    About Array to List, you can use method - asList in Class - java.util.Arrays.
    Great, have you read what was posted hours earlier in this thread or do you simply want to dump the url of your blog?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  3. Replies: 1
    Last Post: 12-03-2009, 07:03 AM
  4. How to link a Array elemant to a Linked list Node
    By ravinda in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 09:16 AM
  5. linked list or array?
    By sick_peng in forum New To Java
    Replies: 6
    Last Post: 04-15-2009, 07:33 PM

Posting Permissions

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