Results 1 to 3 of 3
Thread: Single to multi-dimension array
- 04-05-2011, 05:36 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Single to multi-dimension array
I read file line by line, which, for example, looks like this:
array dimensions should look line that [line number][element in line]Java Code:0 5 6 3 5 9 7 1 2 9 4 3
Here's the code I'm using
But for loop is giving me "java.lang.ArrayIndexOutOfBoundsException: 0" error and I don't see why I should be getting it.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();
I tried using lots of different approaches, but ended up just wasting my time.
- 04-05-2011, 05:45 PM #2
All array is immutable in Java. You need set some size your array or use Collections.
Skype: petrarsentev
http://TrackStudio.com
- 04-05-2011, 06:12 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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
-
array to single char
By rfviki in forum New To JavaReplies: 5Last Post: 11-04-2010, 02:58 PM -
[SOLVED] how to align JTextFiels[dimension] and JComboBox[dimension]
By Wolverine in forum AWT / SwingReplies: 5Last Post: 05-18-2009, 12:42 PM -
noob: two-dimension array
By bobmasta5 in forum New To JavaReplies: 5Last Post: 03-15-2009, 11:42 PM -
Single and Multi Selection Lists
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:03 PM -
Arraylist to a 2- dimension array conversion
By mars123 in forum New To JavaReplies: 1Last Post: 12-06-2007, 11:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks