Results 1 to 3 of 3
  1. #1
    Migy is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Single to multi-dimension array

    I read file line by line, which, for example, looks like this:
    Java Code:
    0 5 6 3
    5 9 7 1
    2 9 4 3
    array dimensions should look line that [line number][element in line]

    Here's the code I'm using
    Java Code:
            FileReader fr = new FileReader("out.txt"); 
            BufferedReader br2 = new BufferedReader(fr); 
            String line; 
            String[] temp = {};
            String[][] temp2 = {{},{}};
            int line_nr = 0;
            while((line = br2.readLine()) != null)
            { 
            	temp = line.split(" ");
            	for(int i=0; i<=temp.length-1; i++)
            	{
            		temp2[line_nr][i] = temp[i];
            	}
            	line_nr++;
            } 
            fr.close();
    But for loop is giving me "java.lang.ArrayIndexOutOfBoundsException: 0" error and I don't see why I should be getting it.
    I tried using lots of different approaches, but ended up just wasting my time.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    10

    Default

    All array is immutable in Java. You need set some size your array or use Collections.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Migy is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Petr View Post
    All array is immutable in Java. You need set some size your array or use Collections.
    I tested single dimension array without setting it's size and it was working just fine ('n still does), so I decided that same would apply to multiple dimensions =/

    Thing is I wouldn't know what size I should set to array, without reading file twice.
    If I haven't been told to use array, I wouldn't be using it =D
    Just tried to escape using shortest way =D

Similar Threads

  1. array to single char
    By rfviki in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 02:58 PM
  2. Replies: 5
    Last Post: 05-18-2009, 12:42 PM
  3. noob: two-dimension array
    By bobmasta5 in forum New To Java
    Replies: 5
    Last Post: 03-15-2009, 11:42 PM
  4. Single and Multi Selection Lists
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:03 PM
  5. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:24 AM

Posting Permissions

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