Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 5th, 2006, 7:31 AM   #1
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
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
deanosrs is offline   Reply With Quote
Old Nov 5th, 2006, 7:59 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 5th, 2006, 8:21 AM   #3
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
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 is offline   Reply With Quote
Old Nov 5th, 2006, 8:22 AM   #4
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
file attached...

thanks for anyone who takes the time to look at this!
Attached Files
File Type: zip bla.zip (1.0 KB, 13 views)
deanosrs is offline   Reply With Quote
Old Nov 5th, 2006, 9:42 AM   #5
_James_
Newbie
 
Join Date: Sep 2006
Location: UK
Posts: 20
Rep Power: 0 _James_ is an unknown quantity at this point
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?
_James_ is offline   Reply With Quote
Old Nov 5th, 2006, 9:49 AM   #6
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
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!
deanosrs is offline   Reply With Quote
Old Nov 5th, 2006, 9:52 AM   #7
_James_
Newbie
 
Join Date: Sep 2006
Location: UK
Posts: 20
Rep Power: 0 _James_ is an unknown quantity at this point
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_ is offline   Reply With Quote
Old Nov 5th, 2006, 10:12 AM   #8
_James_
Newbie
 
Join Date: Sep 2006
Location: UK
Posts: 20
Rep Power: 0 _James_ is an unknown quantity at this point
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);
_James_ is offline   Reply With Quote
Old Nov 5th, 2006, 10:12 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by deanosrs View Post
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:
java Syntax (Toggle Plain Text)
  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.
Arevos is offline   Reply With Quote
Old Nov 5th, 2006, 12:59 PM   #10
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
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.
Attached Files
File Type: zip Cwk2ProblemX.zip (990 Bytes, 14 views)
deanosrs is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
array of listed lists ELHEK Java 3 Sep 27th, 2006 4:19 PM
Assigning an array of lists deanosrs C 42 Apr 13th, 2006 1:35 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
Changing Array structures to Linked Lists? kalulu C 4 Nov 29th, 2005 6:33 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:54 PM.

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