Results 1 to 3 of 3

Thread: 2D array help

  1. #1
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 2D array help

    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];
        
    	
      }
    }
    the text file map1.txt is attached.
    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
    Attached Files Attached Files

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    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?
    Java Code:
    		char[][] mArray = new char[list.size()][];
    		for (int i = 0; i < mArray.length; i++) {
    			mArray[i] = ((String) list.get(i)).toCharArray();
    		}
    :D

  3. #3
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    brilliant, think i have sorted this out now

    thanks very much

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •