Results 1 to 11 of 11
- 04-18-2010, 05:19 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
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:
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.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];}
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 :).
-
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));
- 04-18-2010, 05:32 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
- 04-18-2010, 05:38 AM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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 :)
- 04-18-2010, 05:44 AM #5
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
That is all because you put out an array instead of its elementThe output is usually something like [I@68916a2.If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
- 04-18-2010, 06:11 AM #6
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
Someone suggested me to use this:
It worked, but I don't understand what the for loop is doing here... I'm not good at tracing loops either.Java Code:public static int[] extractRow(int [][] array1, int n){ for (int row = 0; row<array1.length; row++){ if (n == array1[n])
- 04-18-2010, 06:19 AM #7
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
- 04-18-2010, 06:20 AM #8
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
- 04-18-2010, 06:22 AM #9
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
It means that array1[n] is supposed to contain the array indexes :) But it is no good to use the code style :(if (n == array1[n])If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
- 04-18-2010, 06:25 AM #10
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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 :)
- 04-18-2010, 07:56 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 06:10 PM -
extracting bits from big numbers
By ankitmcgill in forum New To JavaReplies: 6Last Post: 05-05-2009, 04:36 AM -
[SOLVED] Extracting Tokens Help
By Hunkpapa in forum New To JavaReplies: 2Last Post: 11-04-2008, 01:17 AM -
Extracting JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:17 AM -
Extracting data from an XML file...
By techno_brains in forum New To JavaReplies: 1Last Post: 07-15-2007, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks