0

I was asked the same question for c# here and I figured out by using linq you can do this easily.

But how can I simply do this since there is no alternative for linq in java?

8
  • 1
    seems like duplicate question. refer this link -> stackoverflow.com/questions/29452190/… Commented Dec 31, 2016 at 19:36
  • You could use a sorting algorithm, which would make the first element of your array the min, and the last element the max. You can also look into using lambda expressions, if you convert your array to be a List. Commented Dec 31, 2016 at 19:46
  • This is a good beginner programming problem. Why don't you give it a try? Start with 1d array first. Commented Dec 31, 2016 at 19:59
  • @Teja I don't think so, i have a list of 2d-arrays Commented Jan 4, 2017 at 16:37
  • @LoganKulinski I did the first solution cause unfortunately my IDE doesn't support lanbda expressions. Commented Jan 4, 2017 at 16:40

2 Answers 2

1

If you want something similar to LINQ, you can do this using Java8 streams:

// prepare arrays
List<int[][]> kidsL = new ArrayList<>();
int[][] square1 = new int[8][8];
int[][] square2 = new int[8][8];
int[][] square3 = new int[8][8];
for (int i = 0; i < 8; i++)
    for (int j = 0; j < 8; j++) {
        square1[i][j] = 1;
        square2[i][j] = 2;
        square3[i][j] = 3;
    }
kidsL.add(square1);
kidsL.add(square2);
kidsL.add(square3);

// find min
kidsL.stream()
        .mapToInt(a -> Arrays.stream(a).flatMapToInt(Arrays::stream).sum())
        .min()
        .ifPresent(System.err::println);

// find max
kidsL.stream()
        .mapToInt(a -> Arrays.stream(a).flatMapToInt(Arrays::stream).sum())
        .max()
        .ifPresent(System.err::println);

It prints:

64
192

But it is less effective than simple for loops, because you calculating the sum two times.

Of course you can add intermediate operation to store the list of sums, and then calculate min/max based on that.

// intermediate list of sums
List<Integer> sums = kidsL.stream()
        .mapToInt(a -> Arrays.stream(a).flatMapToInt(Arrays::stream).sum())
        .boxed().collect(Collectors.toList());

sums.stream().mapToInt(i -> i)
        .min()
        .ifPresent(System.err::println);

sums.stream().mapToInt(i -> i)
        .max()
        .ifPresent(System.err::println);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That's exactly the thing i want. But unfortunately the IDE (Processing Environment) doesn't support lambda expressions...so I'll should go for other solution I guess.
1

If the array is unsorted, the only real way to find the min and max is to iterate through the whole thing.

int[][] array = new int[10][10];
int min = array[0][0];
int max = array[0][0];
for(int i = 0; i < array.length; i++){
    for(int j = 0; j < array[0].length; j++){
        if(array[i][j] < min){
            min = array[i][j];
        }
        if(array[i][j] > max){
            max = array[i][j];
        }
    }
}  

I just reread the question, and I think you mean an ArrayList<int[][]> which would be:

ArrayList<int[][]> arrayList = new ArrayList<int[][]>();

int min = arrayList.get(0)[0][0];
int max = arrayList.get(0)[0][0];

for(int[][] array : arrayList){
    for(int i = 0; i < array.length; i++){
        for(int j = 0; j < array[0].length; j++){
            if(array[i][j] < min){
                min = array[i][j];
            }
            if(array[i][j] > max){
                max = array[i][j];
            }
        }
    }
}

1 Comment

However it's not exactly what i want.(I need to store sum of each 2d-arrays in a list and then find the max and min in that list). But with some modification, I solved my problem thanks to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.