Results 1 to 4 of 4
- 10-27-2011, 04:11 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
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
- 10-27-2011, 04:26 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Re: Storing objects in an array list?
This might help you.
Hope it helped.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 } }
- 10-27-2011, 04:38 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: Storing objects in an array list?
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.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; }
Thanks for you help, any more advice as to my adding and creating the objects problem as described above?
- 10-27-2011, 06:39 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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.
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.Java Code:Process p = new Process(inputs);
Similar Threads
-
Performance issue adding and removing lots of objects to Array list, need better way.
By Neilos in forum New To JavaReplies: 6Last Post: 09-05-2011, 01:25 PM -
Storing objects
By paul1024 in forum New To JavaReplies: 5Last Post: 04-30-2011, 06:12 AM -
Accessing objects in array list
By kev670 in forum New To JavaReplies: 6Last Post: 03-11-2011, 12:49 AM -
arraylist help please? trying to add objects to an array list
By zhangster in forum New To JavaReplies: 9Last Post: 02-10-2010, 03:19 AM -
Storing objects directly with db4o
By german in forum JDBCReplies: 0Last Post: 05-12-2009, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks