Results 1 to 3 of 3
Thread: 2D array help
- 02-23-2011, 04:53 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
2D array help
the text file map1.txt is attached.Java Code:import java.io.*; import java.util.*; public class Map{ public static void main(String[] args){ File map = new File("map1.txt"); try{ String line; ArrayList list = new ArrayList(); Scanner scan = new Scanner(map); for(int i = 0;scan.hasNextLine();i++){ line = scan.nextLine(); list.add(line); } System.out.println(list);//demonstrative, to check working, will be removed } catch(Exception e){ System.out.println("invalid file"); } char[][] mArray = new char[//width][//height]; } }
Basically, could someone tell me how i could define the width and height of my 2d array based on the width and height of the map, considering that it needs to read different rectangular maps.
I believe list.size() should work for the width, but it's the height I don't know how to get
- 02-23-2011, 05:21 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
list.size gives you the height not the width or?
Fo the width you could write ((String)list.get(0)).length() (or better use Generics for your List)
But do you need the width?
:DJava Code:char[][] mArray = new char[list.size()][]; for (int i = 0; i < mArray.length; i++) { mArray[i] = ((String) list.get(i)).toCharArray(); }
- 02-23-2011, 05:49 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks