Results 1 to 4 of 4
  1. #1
    Moedig is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Storing objects in an array list?

    Hi everyone, I'm new to the forums and im learning java, I just need some help on storing some objects in an array list.
    So far i have a class called 'Process' which has two objects in, processID and timeRequired.

    I also have another class called Scheduler that has to be able to store the objects from the process class in an array list and then I need to sort that array list (using collection.sort I presume) in descending order. However I am unsure on the proper syntax to do such a thing. So far I have in my shceduler class:

    package javapackage1;

    import java.util.ArrayList;

    public class Scheduler {
    ArrayList<Process> store;



    }

    And now I'm drawing a blank, could any body point me in the right direction or give me some help, thanks in advance,

    Moedig

  2. #2
    DionnV is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Storing objects in an array list?

    This might help you.

    Java Code:
    package javapackage1;
    
    import java.util.ArrayList;
    
    public class Scheduler {
    ArrayList<Process> store = new ArrayList<Process>(1);
    //the 1 indicates the length of the array; in this case 2. store[0] and store[1]
    //ArrayList has an add() method, therefore;
    store.add(process1);
    store.add(process2);
    
    //Now both processes are added to the array
    
    //Just for checking, the main method is added
    public static void main(String[] args){
    System.out.println(store.contains(process1) + ", " + store.contains(process2));
    //which will return "true, true" if it worked
    }
    }
    Hope it helped.

  3. #3
    Moedig is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Storing objects in an array list?

    Java Code:
    package javapackage1;
    
    import java.util.ArrayList;
    
    public class Scheduler { 
       private ArrayList<Process> store = new ArrayList<Process>();
       
       public Scheduler(ArrayList<Process> store){
           this.store = store;
       }
    I've moved on to this so far, thanks for your help, now I need to make methods to add the objects into the arraylist which I have seen, but I'm unsure how to add a process object then create a new one and add it to the arraylist, also I have no upper or lower boundaries for the amount of elements that could be in this arraylist so it could be 0 or a billion lol. and they are all coming from a seperate class. I think I know how to sort them anyway and can do a couple of loops to bubble sort them into descending order.

    Thanks for you help, any more advice as to my adding and creating the objects problem as described above?

  4. #4
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Storing objects in an array list?

    You would create a Process object the same way you create a String object, or any other object.
    Java Code:
    Process p = new Process(inputs);
    If you are adding processes from within another class, you will need an addProcess() method in this one. That method would simply add to an array list as described in post 2.

Similar Threads

  1. Replies: 6
    Last Post: 09-05-2011, 01:25 PM
  2. Storing objects
    By paul1024 in forum New To Java
    Replies: 5
    Last Post: 04-30-2011, 06:12 AM
  3. Accessing objects in array list
    By kev670 in forum New To Java
    Replies: 6
    Last Post: 03-11-2011, 12:49 AM
  4. Replies: 9
    Last Post: 02-10-2010, 03:19 AM
  5. Storing objects directly with db4o
    By german in forum JDBC
    Replies: 0
    Last Post: 05-12-2009, 08:22 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
  •