Results 1 to 11 of 11
  1. #1
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Extracting a Row/Column from An Array

    I need to extract a row given [il]n[/il], print it out horizontally on the screen, and return it as a 1D-Array.

    Here's my code so far:
    Java Code:
    	public static int[] extractRow(int [][] array1, int n){
    		for (int row = 0; row<=array1.length; row++){
    				System.out.println(array1[n] + "\t");}
    		return array1[n];}
    In main, I made an input scanner where the user will enter an integer from 1-10, called n, which is being passed to that method.

    The output is usually something like [I@68916a2.

    Any ideas? Once I get this finished, I can do the same thing for a column, and I'll be done :).

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What you're seeing is the default toString() result from an array. To print out a one-dimensional array, either loop through it, printing each item as you loop, use the Arrays utility class's static method toString:

    Java Code:
    System.out.println(java.util.Arrays.toString(myArray));

  3. #3
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What you're seeing is the default toString() result from an array. To print out a one-dimensional array, either loop through it, printing each item as you loop, use the Arrays utility class's static method toString:

    Java Code:
    System.out.println(java.util.Arrays.toString(myArray));
    I don't understand how to do either one of those. I've never used the second. The first one sounds easier, but I don't know what to write out.

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    well, first of all you want to extract int [][] array :)

    you should use two loops to scan [][] array ;)

    Modify your code as I did :D

    Java Code:
    public static int[] extractRow(int [][] array1, int n){
    		for (int row = 0; row<=array1.length; row++){
    		for(int j=0; j<array1[row].length;j++)
                                  {
                                  System.out.println(array1[row][j] + "\t");
                                  }		
                                 
                           }
    		return array1[n];
                         }
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    The output is usually something like [I@68916a2.
    That is all because you put out an array instead of its element
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  6. #6
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Someone suggested me to use this:
    Java Code:
            public static int[] extractRow(int [][] array1, int n){
                    for (int row = 0; row<array1.length; row++){
                            if (n == array1[n])
    It worked, but I don't understand what the for loop is doing here... I'm not good at tracing loops either.

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by besweeet View Post
    Someone suggested me to use this:
    Java Code:
            public static int[] extractRow(int [][] array1, int n){
                    for (int row = 0; row<array1.length; row++){
                            if (n == array1[n])
    It worked, but I don't understand what the for loop is doing here... I'm not good at tracing loops either.
    To scan and show each array element ;)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  8. #8
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Webuser View Post
    To scan and show each array element ;)
    What do you mean by "each array element"? Sorry for being such a noob... It's just that I am one :cool:.

  9. #9
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    if (n == array1[n])
    It means that array1[n] is supposed to contain the array indexes :) But it is no good to use the code style :(
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    you should better use the loop iterator instead ;)

    if(n==i)...
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    well, first of all you want to extract int [][] array :)

    you should use two loops to scan [][] array ;)

    Modify your code as I did :D

    Java Code:
    public static int[] extractRow(int [][] array1, int n){
    		for (int row = 0; row<=array1.length; row++){
    		for(int j=0; j<array1[row].length;j++)
                                  {
                                  System.out.println(array1[row][j] + "\t");
                                  }		
                                 
                           }
    		return array1[n];
                         }
    This is utter nonsense; extracting row i (0 <= i < n) from a two dimensional array a[n][m] is simply a[i]. You need just one loop to print out all the elements of the row or use a static Arrays.toString(a[i]) call; btw, please don't beg for rep points.

    kind regards,

    Jos

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 07:10 PM
  2. extracting bits from big numbers
    By ankitmcgill in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 05:36 AM
  3. [SOLVED] Extracting Tokens Help
    By Hunkpapa in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 02:17 AM
  4. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:17 AM
  5. Extracting data from an XML file...
    By techno_brains in forum New To Java
    Replies: 1
    Last Post: 07-15-2007, 06:46 PM

Posting Permissions

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