Java中用正确数据结构取数组最大值及getUnits方法实现咨询
Hey there! Let's break this down step by step to solve your questions.
There are a few clean, idiomatic ways to do this depending on your Java version and preferences:
Using Streams (Java 8+)
This is the most concise approach for modern Java—it handles iteration and max calculation out of the box:
import java.util.Arrays; public int findMaxElement(int[] array) { if (array == null || array.length == 0) { throw new IllegalArgumentException("Array can't be empty or null"); } return Arrays.stream(array).max().getAsInt(); }
Don’t forget to handle empty/null arrays to avoid a NoSuchElementException.
Traditional Loop Approach
If you prefer a more explicit, pre-Java 8 method, manually iterate and track the maximum value:
public int findMaxElement(int[] array) { if (array == null || array.length == 0) { throw new IllegalArgumentException("Array can't be empty or null"); } int max = array[0]; for (int num : array) { if (num > max) { max = num; } } return max; }
Using Collections (If Converting to a List)
You can convert the array to a list of wrapper integers and use Collections.max():
import java.util.Arrays; import java.util.Collections; import java.util.List; public int findMaxElement(int[] array) { if (array == null || array.length == 0) { throw new IllegalArgumentException("Array can't be empty or null"); } List<Integer> integerList = Arrays.stream(array).boxed().toList(); return Collections.max(integerList); }
getUnits Method Looking at your code snippet, it seems you’re trying to track the highest Units value returned by getAdditionalUnits() across all elements in list2values. Let’s clean this up and make it robust:
First, I’ll assume getAdditionalUnits() returns an array where index 1 corresponds to the Units value (since your code was cut off at currentUnits = curr...). Here’s the corrected method:
private int getUnits(List<String> list1values, List<String> list2values) { // Start with the lowest possible integer if Units can be negative; use 0 only if values are non-negative int highestUnits = Integer.MIN_VALUE; for (String listValue : list2values) { int[] currentHitsArray = getAdditionalUnits(list1values, listValue); // Guard against null or incomplete arrays to avoid index out-of-bounds errors if (currentHitsArray == null || currentHitsArray.length < 2) { continue; // Or throw an exception if this should never happen in your logic } int currentUnits = currentHitsArray[1]; // Update highestUnits if current value is larger if (currentUnits > highestUnits) { highestUnits = currentUnits; } } // Handle edge case where all entries were invalid (adjust based on your business needs) return highestUnits == Integer.MIN_VALUE ? 0 : highestUnits; }
Key Notes:
- Initialization: If your
Unitscan ever be negative, starting withInteger.MIN_VALUEensures you don’t miss the actual maximum. If values are always non-negative,0is a safe starting point. - Array Validation: Always check if the returned array is null or too short—this prevents nasty
ArrayIndexOutOfBoundsExceptioncrashes. - Custom Logic Adjustment: If you need to prioritize the highest
Hitvalue first (e.g., only updateUnitsif the correspondingHitis higher than the current highest), adjust the comparison like this:int highestHit = Integer.MIN_VALUE; int highestUnits = 0; // Inside the loop: int currentHit = currentHitsArray[0]; int currentUnits = currentHitsArray[1]; if (currentHit > highestHit || (currentHit == highestHit && currentUnits > highestUnits)) { highestHit = currentHit; highestUnits = currentUnits; }
内容的提问来源于stack exchange,提问作者ronan

