简体   繁体   中英

Java Finding Max Element

I'm attempting to find the maximum element without using

import java.util.Collections

public static int maxArrayListValue(int[] arrayList) {
    int maxVal = Integer.MIN_VALUE;
    for (int[] qqq : arrayList) {
        for (int vvv : qqq) {
            if (vvv > maxVal) {
                maxVal = vvv;
            }
        }
    }
    return maxVal;
}

But I always get an error in for (int[] qqq : arrayList)

(Type mismatch: cannot convert from element type int to int[])

After Changing it to " for (int qqq : arrayList) " Still got an error on the second code for (int vvv : qqq)

(Can only iterate over an array or an instance of java.lang.Iterable)

Enhanced for loop takes each value from the provided Collection and processes it.

Your code should be:

public static int maxArrayListValue(int[] arrayList) {
    int maxVal = Integer.MIN_VALUE;
    for (int vvv : arrayList) {
        if (vvv > maxVal) {
            maxVal = vvv;
        }
    }
    return maxVal;
}

arrayList is a 1-D array , so for (int[] qqq : arrayList) is wrong

use for (int qqq : arrayList)

Try this:

public static int maxArrayListValue(int[] arrayList) {
    int maxVal = Integer.MIN_VALUE;
    for (int qqq : arrayList) {
        if(qqq > maxVal){
            maxVal = qqq;
        }
    }
    return maxVal;
}

You're declaring the variable qqq as an array of Integers.
The reason this declaration is incorrect is because arrayList is a one dimensional array meaning it contains Integers and not arrays of Integers.
Assuming that you wish to check for each integer within the arrayList parameter in the method, you'll need to replace the int[] declaration with int so that it's checking for each Integer rather than for each array of Integers.

Regarding the other error, that nested for loop isn't required. By saying:

for (int vvv : qqq)

with qqq declared as an Integer, you're essentially saying "for each integer in an integer" which doesn't make sense - hence the error. You can use qqq instead of vvv where you're checking the validity of it against the maxVal .

To fix both problems you're having, here's the corrected method:

public static int maxArrayListValue(int[] arrayList) {
    int maxVal = Integer.MIN_VALUE;
    for (int vvv : arrayList) {
        if (vvv > maxVal) {
            maxVal = vvv;
        }
    }
    return maxVal;
}

Try this:

public static int maxArrayListValue(int[] arrayList) {
    int maxVal = 0;
    for (int[] i = 0; i < arrayList.length-1; i ++) {
            if (arrayList[i] > maxVal) {
                maxVal = vvv;
            }
    }
    return maxVal;
}

How about using Streams? Though an int[] won't work, consider using Integer[] or directly use a List<Integer>

Arrays.asList(arrayList).stream()
      .sorted(Comparator.reverseOrder())
      .collect(Collectors.toList()).get(0);

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