Array of Linked List

Reading the data from a file and creating an array of linked list

Page 1 of 1

5 Replies - 5769 Views - Last Post: 03 December 2009 - 12:09 PM Rate Topic: -----

#1 kool001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 09

Array of Linked List

Posted 02 December 2009 - 08:56 PM

I initially have to create a weighted graph reading from a text file the vertices, and edges( edges like "a b 5" where 'a' is the source, 'b' is the destination and 5 is the weight of the edge)

I have to create an adjacency list of the graph. So after reading data from the input file it has to create an array of linked lists!

Could anybody help me on this?

This post has been edited by kool001: 02 December 2009 - 10:03 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Array of Linked List

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Array of Linked List

Posted 02 December 2009 - 09:54 PM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
Was This Post Helpful? 0
  • +
  • -

#3 kool001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 09

Re: Array of Linked List

Posted 02 December 2009 - 10:07 PM

I dont know how to use an array of linked lists by reading the data from a file. i havent asked the code. I am just asking how do we implement it?

i have a brief idea when we enter the input during program execution. But reading the data from file.... i am confused
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Array of Linked List

Posted 02 December 2009 - 10:16 PM

View Postkool001, on 2 Dec, 2009 - 09:07 PM, said:

I dont know how to use an array of linked lists by reading the data from a file. i havent asked the code. I am just asking how do we implement it?

i have a brief idea when we enter the input during program execution. But reading the data from file.... i am confused

Start by showing us how you would do it with a single linked list then we'll see if you need an array of them
Was This Post Helpful? 0
  • +
  • -

#5 kool001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 09

Re: Array of Linked List

Posted 02 December 2009 - 11:41 PM

I m stuck here. I m trying to store some vertices(4) in an array. and trying to read them. But i m getting an error of "Array Index out of Bounds Exception.
I know the error is in the line i have highlighted(red). And i have tried various methods. But not working.

 public void readInput()
   {
	  Scanner input = null;
	 try
	  { 
	   input = new Scanner(new FileInputStream("Test1.txt"));
		int vertex = input.nextInt();
		System.out.println("printing # of vertex "+vertex); 
 	   String[] array = new String[vertex];
		System.out.println("array size "+ array.length);
		
		// reading vertex from file and storing them in the array
		for(int i=0;i<=array.length;i++) 
		 {
		  array[i]=input.nextLine();
		  System.out.println("vertex "+array[i]);
		 } 
		 




the error that i get is
----jGRASP exec: java DriverProgram

returned from absgraph
printing # of vertex 4
array size 4
vertex 
vertex a
vertex b
vertex c
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at AbsGraph.readInput(AbsGraph.java:80)
	at DriverProgram.main(DriverProgram.java:12)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.




This post has been edited by kool001: 02 December 2009 - 11:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 pulpCoder  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 25
  • Joined: 02-December 09

Re: Array of Linked List

Posted 03 December 2009 - 12:09 PM

Your for loop should go while < array.length, not <= array.length. Array.length is the number of elements in the array which starts counting at 1, but the indices of the array start at 0, so your last index is array.length - 1;
Try this
for(int i=0;i<array.length;i++) 
{
		  array[i]=input.nextLine();
		  System.out.println("vertex "+array[i]);
}



Edit: Wrote 1 - array.length at first by accident ><

This post has been edited by pulpCoder: 03 December 2009 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1