-
Any Java Gurus?
Sorry about a java question on a c++ board, but this is the best place I've found to get some answers.
I'm sure the answer won't be much diifferent anyway. lol
I am trying to create a 2 dimensional LinkedList . How do I do this??!!!!!!
Part of my code:
// list is a linked list. I am trying to add
// the required amount of queues to it
for (int w = 0; w < num; w++)
{
list.add(new LinkedList());
}
// next, I want to add the elements to
// the first queue and print out the
// results each time.
process = new Process (arrive, service, pid);
list.get(0).add(process);
finalChange = (Process)list.get(0).get(0);
System.out.println (finalChange.pid);
I would have thought that list.get(0) would get me the reference that I needed to access the LinkedList that carries queue #1. The compiler does not seem to like this reference.
Any ideas?
-
atleats put it under general. only c++prgoraming come to this forum. u would have to get lucky to ifn someone who knows both, if you wont a reply post on general
-
You'll probably get flamed for posting a Java question, and redirected to a Java board somewhere else.
You need to remember your casts though. get(0) (as an aside, perhaps you should use getFirst() instead of get(0), it's probably faster) returns an Object, not a LinkedList.
Code:
process = new Process (arrive, service, pid);
// list.get(0).add(process); // Object has no add method
((LinkedList) list.getFirst()).add(process)
// finalChange = (Process)list.get(0).get(0); // same problem
finalChange = (Process) ((LinkedList) list.getFirst()).getFirst()
System.out.println (finalChange.pid);
-
You could probably sneak a java question onto the C# board...although I'm pretty experienced in java and have never been on the C# board.
-
>> I'm pretty experienced in java and have never been on the C# board.
And did you know that MY best quality is that i'm just so modest :D ...
-
Here is a great java forum
http://forum.java.sun.com/
-
Thanks for reponding SilentStrike! I thought for sure your idea would have worked cause it made sense. But, alas, it does not. I will try to post in general. Don't want to make C++ gurus mad.:)
-
What errors does the compiler say?
-
>I am trying to create a 2 dimensional LinkedList . How do I do this??!!!!!!
Forget about the language and go back to the data structure. Imagine a 2 x 2 linked matrix, this can be done very easily in any language that supports a linked data structure. There is one big consideration though, if you only want to traverse the list as you would a two dimensional array, like so:
Code:
a[0][0]
a[0][1]
a[1][0]
a[1][1]
Then you probably would be better off with just a simple linked list since the principle is the same, you go through the list in a linear motion. If, however, your goal is to go from say a[0][0] to a[1][0] without having to go through all of the first row then a linked matrix would be appropriate. To make such a matrix, consider the declaration:
Code:
// Big time pseudocode, I never use Java :P */
public class LinkedMatrix
{
// Data here
LinkedMatrix up;
LinkedMatrix down;
LinkedMatrix left;
LinkedMatrix right;
}
Instead of just a next link, you have four, one for each direction in the matrix. Building the matrix is of course, more difficult. :) You have to consider normal traversal from a[0][1] to a[1][0] as well as any non-linear traversal.
-Prelude