Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   array of lists of ints (http://www.programmingforums.org/showthread.php?t=11806)

deanosrs Nov 5th, 2006 8:31 AM

array of lists of ints
 
Hi,
I'm trying to get a data structure for an adjacency list representation of a graph. So I need an array of lists of ints. I have no idea how to set this up, I've mostly programmed in C and PHP before and Java is quite different and it's got me quite stuck. I would post some code but I don't really have any right now! Just hoping someone can give me a pointer or two.
Thanks
Simon

DaWei Nov 5th, 2006 8:59 AM

An array of lists of ints is a fairly simple abstraction. Exactly how are you stuck? Syntax? Overall use of Java as it tends to be implemented today? Your potential respondents could use a pointer or two, also.

deanosrs Nov 5th, 2006 9:21 AM

Ok right I've managed to sort myself out with it now - was in a major panic earlier.

I now have the following segments of code:

private LinkedList[] graph = new LinkedList[100];

and to add data to it where src and dest are ints indicating the source and destination - the src references the array index and the dest is stored as data in that list:

graph[src].addLast(dest);

However I get a warning with this code saying "unchecked call to addLast(E)". I found a webpage here, http://www.rgagnon.com/javadetails/java-0521.html , that seems to suggest I need to put <int> in a couple of places in my first line of code defining the data structure. I can't seem to get it anywhere without it giving me an error and not compiling though. I'll attach my source file in a sec...

deanosrs Nov 5th, 2006 9:22 AM

1 Attachment(s)
file attached...

thanks for anyone who takes the time to look at this!

_James_ Nov 5th, 2006 10:42 AM

LinkedList is a generic class so...

You create an array or LinkedList references so then you need to go and initialize the array in a loop i think.
:


LikedList<int>[] graph = new LinkedList<int>[100];

for(int i = 0; i != graph.length(); i++)
{
    graph[i] = new LinkedList<int>();
}

//now you can add stuff to it

graph[0].addLast(9999);


I think the code above is roughly correct, but with generics in java i am never sure if my syntax is right.

Dont kill me if its not coz i feel like i have been injested into a jet engine and blasted out the other end.

One a side note. Do you go to Bristol or UWE?

deanosrs Nov 5th, 2006 10:49 AM

See when I put the <int>'s there I get the error: "found: int, required: reference" and also "generic array creation", I really don't know whats going on with it...

I'm Bristol 2nd year, and seriously regretting my choice of course - love web design and the high level coding to bits, but all this software engineering is doing my head in!

_James_ Nov 5th, 2006 10:52 AM

Im at uwe doing cs... Need i say more lol.

I will fire up Netbeans in a bit and see what i can see....

Probably someone else will come up with something by then.

_James_ Nov 5th, 2006 11:12 AM

I think i may have got it...

Generics in java are retarded so it took me a while to work it out

This appears to work but dont kill me if it blows the whole java VM up.

Note: this only works with Java 1.5

:

        LinkedList[] graph = new LinkedList[100];
        for(int i = 0; i != graph.length; i++)
        {
            //Java needs a class as a type paramater not a primative
            // so i used Integer. It should autobox/unbox for you.
            graph[i] = new LinkedList<Integer>();
        }
        //this appears to work without thowing an exception.
        graph[0].addLast(99);


Arevos Nov 5th, 2006 11:12 AM

Quote:

Originally Posted by deanosrs (Post 118391)
See when I put the <int>'s there I get the error: "found: int, required: reference" and also "generic array creation", I really don't know whats going on with it...

What version of Java are you using? The <object> syntax only works with version 1.5. (edit: Oh, and it seems like Java's autoboxing doesn't extend to generic typing, so it's <Integer> and not <int>, as _James_ points out)

Also, there isn't any code in which you instantiate your LinkedLists. Arrays of objects are created empty. You have to populate them first. For instance:
:

  1. LinkedList[] graph = new LinkedList[100]; // empty array of type LinkedList
  2.  
  3. for (int i = 0; i < graph.length; i++)    // populate array with LinkedLists
  4.     graph[i] = new LinkedList();

This is something that frequently confuses people. Personally, I don't think Java's the best language to teach people with, as it does have a few unintuitive parts to it for those unfamiliar with low level languages.

deanosrs Nov 5th, 2006 1:59 PM

1 Attachment(s)
So I copy and pasted and it worked with the stuff from James' post... but since I've had to change my code and now it is doing it again! I've got a class called Edge now which just stores a destination node and the edge's weight. So my code is:

Quote:

ArrayList[] graph = new ArrayList[nodeno];
for (j = 0; j < nodeno; j++) graph[j] = new ArrayList<Edge>();
when initialising the array of arraylists (switched from linked lists!),

and when adding to it,

Quote:

addthis = new Edge(dest,weight);
graph[src - 1].add(addthis);
The full source is attached... I've got the same loop in though, so I can't see why I'm still getting the warning.


All times are GMT -5. The time now is 1:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC