Results 1 to 2 of 2
  1. #1
    kool001 is offline Member
    Join Date
    Dec 2009
    Rep Power

    Question How to create an array of linked list reading data from a file


    I need to create a graph such that i read the number of vertices from a file and the edges too. Example "a b 5"--> means theres an edge from source 'a', destination 'b' and the edge weight is '5'. If i have some random number of vertices say 5, i need to create an adjacency list of the graph, using an array of linked lists. But i dont know how to use it!!

    Could anybody help me with it?
    Last edited by kool001; 12-03-2009 at 05:17 AM.

  2. #2
    kool001 is offline Member
    Join Date
    Dec 2009
    Rep Power


    Well, here i m trying store the 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.

    Java Code:
    public void readInput()
    	  Scanner input = null;
    	   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++) 
    		  System.out.println("vertex "+array[i]);
    the error that i get is

    Java Code:
    returned from absgraph
    printing # of vertex 4
    array size 4
    vertex a
    vertex b
    vertex c
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    	at AbsGraph.readInput(
    	at DriverProgram.main(
     ----jGRASP wedge2: exit code for process is 1.
    what fuction should i use?

Similar Threads

  1. How to link a Array elemant to a Linked list Node
    By ravinda in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 09:16 AM
  2. linked list or array?
    By sick_peng in forum New To Java
    Replies: 6
    Last Post: 04-15-2009, 07:33 PM
  3. Replies: 0
    Last Post: 03-31-2009, 04:52 PM
  4. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts