![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
|
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... |
|
|
|
|
|
#4 |
|
Programmer
|
file attached...
thanks for anyone who takes the time to look at this! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2006
Location: UK
Posts: 20
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#6 |
|
Programmer
|
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! |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2006
Location: UK
Posts: 20
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Sep 2006
Location: UK
Posts: 20
Rep Power: 0
![]() |
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); |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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: java Syntax (Toggle Plain Text)
|
|
|
|
|
|
|
#10 | ||
|
Programmer
|
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:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| array of listed lists | ELHEK | Java | 3 | Sep 27th, 2006 5:19 PM |
| Assigning an array of lists | deanosrs | C | 42 | Apr 13th, 2006 2:35 PM |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 9:19 PM |
| Changing Array structures to Linked Lists? | kalulu | C | 4 | Nov 29th, 2005 7:33 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 3:36 AM |