Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Uneven 2D Array

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java Uneven 2D Array

    Hi,

    Just wondering if anyone has any example of reading data from a file and storing it into an uneven 2D array.

    For example:
    the file contains
    [a,b,c]
    [a,b,c,d]
    [a,b,]
    etc

    I am able to do this with a complete "square" or matrix but when the columns aren't even I'm not sure how to do this.

    Code below I initialised the array as 3 rows and 4 columns but my column can be less than that. How do I input the data into the matrix if column is only 2?

    	    File file = new File("testing.txt");
     
    	    String[][] list = new String[3][4];
     
    	    try {
    	    	Scanner scanner = new Scanner(file).useDelimiter(",|\r"); 		
    	    		for (int r=0; r < 3; r++)
    	    		{
    	    			for (int c=0; c <4; c++)
    	    			{
     
    	    				list[r][c] = scanner.next();
    	    			}
    	    		}
    	    }
    	    catch (FileNotFoundException e) {
                e.printStackTrace();
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Uneven 2D Array

    Create the second dim part of the array as you need it:
    something like this
       String[][] anArray= new String[4][];  // leave second dims undefined 
         ...
       anArray[i] = new String[size];    // define the size of second dim here
    Last edited by Norm; August 9th, 2011 at 09:00 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ba.accounts (August 10th, 2011)

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Uneven 2D Array

    A 2D array is just an array of arrays. The arrays can be of any length you want. You don't have to specify the size of both dimensions when you initialize the whole thing. Initialize the second dimension when you figure out the size you want it to be.

    Or just use a List like a sane person.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ba.accounts (August 10th, 2011)

  6. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Java Uneven 2D Array

    Java does not have multi-dimensional (2D, 3D) arrays, it only has arrays of references. Those references can be references to other arrays, so you can build up arbitrarily-nested arrays-of-arrays, but you'd have to implement your own 2D abstract data type if you really wanted one. Having said that, you obviously don't want a 2D array!

    I don't ever use Scanner - I keep meaning to, but habitually use plain old BufferedReader. In your place I would read that file a line at a time (with BufferedReader.readLine()) and use String.split(",") to convert the comma-separated fields into an array of String. That would give you a length for your new int array, which you could then instantiate, add to a List (because you don't know how many lines in the file?) of such arrays before finally converting to an array of arrays of int with List.toArray().

  7. The Following User Says Thank You to Sean4u For This Useful Post:

    ba.accounts (August 10th, 2011)

  8. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Uneven 2D Array

    Cross posted at Java Uneven 2D Array - Java

  9. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Uneven 2D Array

    Thanks for your help guys! managed to get my code working.

Similar Threads

  1. returning 2d array in java
    By dr.code.skm in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 10:14 AM
  2. Java Array
    By lary in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 09:29 AM
  3. Help needed on java array
    By rossfally in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2010, 08:49 PM
  4. Java Webservice problem (Array)
    By Gadge in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2010, 11:06 AM
  5. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: December 1st, 2008, 09:02 AM