简体   繁体   中英

How to pass a 2D Array from one class to be used in another as a parameter so that they can be sorted into 2 ArrayLists

I have a class with a 2D array which stores values that have been randomly generated by a method. I want to use this 2D array to pass its values to be used by the 'GenerateGradesUranium()' method in another class.

The class with the array: (To my understanding, this Class is fine for the purpose I want it to achieve).

public class GenerateUranium
{
int[][] tray = new int[5][3];
private int grading = 0;
private Random randomGenerator;

public GenerateUranium()
{
randomGenerator = new Random();
}

public void generateSample()
{
for (int i = 0; i < tray.length; i++)
{
  for (int j = 0; j < tray[i].length; j++)
  {
    grading = randomGenerator.nextInt(50);
            tray[i][j]=grading;
        }
}
printTray(tray);
}

The class with the method that I want to use the 2d array in. (Where I'm completely lost).

import java.util.ArrayList;

public class LithiumGradingUranium
{

private ArrayList highGradeUranium;
private ArrayList lowGradeUranium;


public LithiumGradingUranium()
{

}



public void generateGradesUranium() // This is the method where I want to use the Array as a parameter
{


}

So the 2D array is used as a parameter in 'generateGrades()' and then need to be split between two array lists based on the value being > 25 (highGrade) or ≤ 25 (lowGrade)

I've tried a variety of ways to get the array list from the 1st class to work in the 2nd but they haven't worked. I've looked at book references and video tutorials but they often only deal with non-2d Arrays or using the array within the same class. I've new at Java and I've been thrown in the deep end, I'd really appreciate the help in this so that I can move on to figuring out and completing the rest of my program. Not knowing how to make use of the initial array is crippling my ability to make the program work as intended. Trying to have a 2D array turn into 2 Array Lists further complicates the matter.

Many Thanks

I'm not at a computer and it's been a bit since I've dealt with 2d arrays, but does this not work?

public void generateGrades(int[] [] tray) {
... 
} 

You pass 2D arrays in a similar way you pass 1D arrays say:

public void generateGrades(int[][] tray)
{
    // Now here's some pseudo code on how to iterate the tray
    for(int i = 0; i < tray.length; ++i)
    {
        for(int j = 0; j < tray[0].length; ++j)
        {
            int trayElement = tray[i][j];
            if(trayElement less than 25)
            {
                // add it to the lower grade list
            }
            else
            {
                // add it to the higher grade list
             }
        }
    }
}

One more thing, when using generics don't use what's called raw types eg

ArrayList

Instead specify the type

ArrayList<Integer>

Using java 8 streams, you can also write:

Map<Boolean, List<Integer>> result = 
    Arrays.stream(tray).flatMapToInt(Arrays::stream).boxed()
        .collect(Collectors.groupingBy(d -> d > 25));

will give you a java.util.HashMap with Boolean keys like the following:

{false=[5, 11, 25, 10, 14, 20, 15, 3, 6, 9], true=[38, 43, 38, 28, 40]}

So the low grades are under false key and the high grades are under true key in the map and are easily retrievable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM