![]() |
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 |
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.
|
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... |
1 Attachment(s)
file attached...
thanks for anyone who takes the time to look at this! |
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. :
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? |
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! |
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. |
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]; |
Quote:
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 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:
and when adding to it, Quote:
|
| 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