0

So I'm given a parameter in which the user chooses which row he wants me to print out, how do I print out the row that the user wants?

This is my 2D array: Jewel[][] myGrid;

 public Jewel[] getRow(int row) { 
     return null; 
 }
  • Can you add the code where your 2-d array is shown? – Assafs Oct 30 '17 at 18:55
  • Could you please show me how to do that Aomine? –  Oct 30 '17 at 18:57
  • @Issa You need to add an `@` symbol in front of his name, else he will not be notified and probably not read your comment. – Zabuzard Oct 30 '17 at 19:02
  • Welcome to Stack Overflow! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Oct 30 '17 at 19:04
  • Welcome to Stack Overflow! [Questions asking for *homework help* must include a summary of the work you've done so far to solve the problem, and a detailed description of the difficulty you are having solving it.](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) *does not work*, *please help me* are not acceptable. –  Oct 30 '17 at 19:04
  • this brings help vampirism to a new low –  Oct 30 '17 at 19:05

2 Answers2

-1

run a for loop to cycle through that row by getting the amount of columns in that row. In each loop, get that number from the 2d array and add it to a list. Return that list. I can write the code for you if you need.

for(int i = 0; i < myGrid[row].length; i++){
    System.out.println(myGrid[row][i]);
}
gkgkgkgk
  • 707
  • 2
  • 7
  • 26
  • could you please write the code, it would make it easier for me to understand –  Oct 30 '17 at 18:57
  • @Issa sure. Ill edit it in a minute. – gkgkgkgk Oct 30 '17 at 18:58
  • @Issa let me know if this solves the issue. – gkgkgkgk Oct 30 '17 at 19:01
  • I hope someone gets to take your code and get credit for it and paid for in the very near future. –  Oct 30 '17 at 19:03
  • @gkgkgkgk Why did you make an ArrayList and add that onto myGrid? –  Oct 30 '17 at 19:03
  • @Issa I just organized it into a list so that the method will return the Jewel[] with its contents. The important part is the for loop that cycles through the row in the 2d array. technically you can remove the entire use of the ArrayList and return null, but then you should just make it a regular function without a return type. – gkgkgkgk Oct 30 '17 at 19:06
-1

If the first dimension of your Jewel[][] myGrid is the row index:

public Jewel[] getRow(int row) { 
    return myGrid[row]; 
}

If the second dimension in the row index:

public Jewel[] getRow(int row) { 
    Jewel[] result = new Jewel[myGrid.length];
    for (int i = 0; i < myGrid.length; i++) {
        result[i] = myGrid[i][row];
    }
    return result; 
}

Then you simply call

System.out.println(Arrays.toString(getRow(0)));
Neuron
  • 5,141
  • 5
  • 38
  • 59