I am trying to create a program that is just like a Process Table. I have to implement a class PCB (Process Control Block) with several fields such as:process name (a string)
process priority (an integer)
register set values (an object of a class Register Set containing the following fields: XAR, XDI, XDO, PC.
Then my program needs to then create a Process Table data structure as either an array (max size 100 elements) or an arraylist of type PCB, and initialize the array with data from the file "processes1.txt" Then the Process Table arrraylist has to print out its contents by each process.
So my questions are:
1. How many programs/classes do I have to write? Is it 3. The first program that creates the Process Table arraylist of PCB. The 2nd class would be the PCB class that defines the PCB fields.
2. How would the first program initialize the arraylist with the data from the text file?
3. Could I use an ArrayList of an ArrayList? and how would I do that?
Thank you in advance.
ProcessTable, ProcessControlBlock, RegisterSet sound like good starts.
I'd create a method in ProcessTable called load(File file) that uses File, and perhaps TextReader to read the configuration. There are many ways to read a text file. Also google on BufferedInputStream. Examples abound.
ArrayLists can hold objects, and ArrayList is indeed an Object, so yes. The use is easy: someArrayList.add(someOtherArraylist); though the declaration is a little harder:
ArrayList<ArrayList<String>> a = new ArrayList<ArrayList<String>>();
which says 'a is to be an ArrayList containing other ArrayLists containing Strings.` there are other ways to write the declaration that are a little more general, but this shows the gist.
Related
As a intermediate Java student I have a question regarding retrieving single data pieces with a key from a larger document, uploaded online.
My idea is to create enum objects (public static final) and associate one ArrayList to each of enums in its constructor. All of the enums (with arrayLists) would then be uploaded onto the single larger online server database.
My wish is that every enum would serve as a key to retrieve appropriate (associated) arrayList in such document; meaning I would only like to retrieve arrayList one in a time (based on the enum as a key) and not the (whole) document itself. I would then like to add or remove elements from given arrayList and upload them back online.
How can I do that in the most effective fassion possible?
Thank you for your help and have a nice day :D
As far as my understanding and Assuming you want to use Enum and Arralyslist and store the Arralylist Object in Database.Try following below
You can simply create list from array like this:
List<String> list = Arrays.asList(SomeEnum.values());
Note :- Using JDBC API you can Connect to the DB and Insert the ArrayList Object using Insert query in the Database.
I want to store three values in a 2D type in java. I know that we can use List and ArrayList for storing 1D values but I need to store more than one field in a specific record. For example i have to enter the details for multiple columns i.e. (1,1),(1,2),(1,3) for details such aaaa, bbbb, cccc for a person and store them in one single row(which may consist of values which are other than string type). It should run in a loop and once details of a person is stored, it should store (2,1),(2,2),(2,3) i.e. again for a new person. How to do that?
And later on, how to retrieve and send the complete set to database together? Please help..
What you might want to do is to create a class that holds all of the information you want to keep related to a single record if it represents a concrete thing and use the List and ArrayList to store those.
What I mean by concrete thing is something that has a finite set of information that will stay the same over each object.
Something like:
public class Person
{
String name;
Integer age;
// etc...
}
This gives you two advantages over using something like a 2D array. First, it will make reading your code easier, since instead of having to remember that arrayName[x][0] is whatever you decide the first field is, you can access it using something like listItem.attributeName. The second advantage is that you can abstract out any common datahandling tasks as class methods instead of having to bloat your main class with it.
I am quite new to java currently working on a not-so-simple web browser application in which I would like to record a permanent history file with a 2D array setup with 3 columns containing "Date Viewed", "URL", "How many times this URL has been viewed before".
Currently I have a temporary solution that only saves "URL" which is also used for "Back, Foward" features using an ArrayList.
private List tempHistory = new ArrayList();
I am reading through the Java documentation but I cannot put together a solution, unless I am missing the obvious there is no 2D array as flexible a ArrayList like in Python?
From your description it doesn't sound like you need a 2D array. You just have one dimension -- but of complex data types, right?
So define a HistoryItem class or something with a Date property for date viewed, URL for URL, int for view count.
Then you just want a List<HistoryItem> history = new ArrayList<HistoryItem>().
The reason I don't think you really want a 2D array-like thing is that it could only hold one data type, and you clearly have several data types at work here, like a date and a count. But if you really want a table-like abstraction, try Guava's Table.
No, there is no built-in 2D array type in Java (unless you use primitive arrays).
You could just use a list of lists (List<List>) - however, I think it is almost always better to use a custom type that you put into the list. In your case, you'd create a class HistoryEntry (with fields for "Date viewed", URL etc.), and use List<HistoryEntry>. That way, you get all the benefits a proper type gives you (typechecking, completion in an IDE, ability to put methods into the class etc.).
How do you plan to browse the history then? If you want to search the history for each url later on then ArrayList approach might not be efficient.
I would rather prefer a Map with URL as key.
Map<Url,UrlHistory> browseHistory = new HahMap<Url,UrlHistory> ();
UrlHistory will contains all the fields you want to associate with a url like no. of times page was accessed and all.
Hi i was wondering if this was possible. i have 5 multiple array lists. after i add information to each array lists is it possible to save this as a single file using filechooser. i have 5 arraylist which i have created as a object from a different class.
hotelArrayObjects.getDahabArray()
hotelArrayObjects.getRomeArray()
hotelArrayObjects.getParisArray()
hotelArrayObject.getBerlinArray()
hotelArrayObjects.getNiceArray()
wondering if there was any way to do this. I was thinking of using Filechooser to make it more user friendly and easier to retrieve.
Any tips on how to tackle this problem?
You can use java.io.ObjectOutputStream and its subclasses for writing objects to files. You can read them back with java.io.ObjectInputStream and its subclasses. There are readObject() and writeObject() methods in these classes that you can use. Note that if you want to write an object to a file, the defining class of that object should be serializable. In you case, objects that you store in the ArrayList should be serializable.
Java mechanisms for storing and retrieving objects does not need the file to be selected with a FileChooser. You can use java.io.File class for opening a stream to your file.
If you want to save the information within an arraylist, you can do a few forloops then use a file writer
String s1;
for(int i=0; i<arraylist.length(); i++) {
s1+=arraylist.get(i);
}
repeat repeat
then use something like this:
String finalString = s1+s2+s3+s4+s5;
FileWriter fwrite = new FileWriter("hotels.txt");
BufferedWriter out = new BufferedWriter(fwrite);
out.write(finalString);
Then you can use the JFileChooser to save that file.
I have some domain classes and i want to init and fill those classes with sample hardcode data , is there any method which i can fill data with any framework ?
For Example : List<Customer> should be filled with some mock data
Consider maintaining your test data in a JSON structure, and use a framework (e.g. google-gson) to deserialize the data into value objects.
If you wish to auto-generate random data, you might want to look into something like Quickcheck, which seems to be Java's equivalent of the .NET framework Autofixture.
As #ipavlic wrote, you might make your constructor generate some random data when the object is created.
You may store the data in a DB or a simple text file and read it from there when you fill your list.
You may combine aproach 1 and 2 and store possible field values in a file or somewhere else and fill the Object fields with these randomly chosen predefined values.
If you want fill list of Customer, there is this method Collections.fill(java.util.List, T) to fill list. This method replace current objects in list. If list is empty it won't fill.
You can put your hard-coded data in a constructor.
If it's mocking frameworks that you are after (as you indicate in comments), then take a look at e.g. Mockito.