Java中如何从方法同时返回数组与变量?结果该如何存储?
Hey there! Let's figure out how to return both your int[] array and int variable from the fun() method. Since Java methods can only return one single value directly, your current int[] return type won't work—it can only send back the array, not the integer. Here are three practical solutions to solve this:
1. Create a Custom Result Class (Most Recommended)
This is the cleanest, most readable approach. You'll make a simple class to wrap both your array and integer, so your method can return an instance of this class.
Example Code:
// First, define a class to hold your two return values class MethodResult { private int[] arrayResult; private int intResult; // Constructor to initialize both values public MethodResult(int[] arrayResult, int intResult) { this.arrayResult = arrayResult; this.intResult = intResult; } // Getters to access the values public int[] getArrayResult() { return arrayResult; } public int getIntResult() { return intResult; } } class Return_two{ public static void main(String args[]){ int b=0; int a[] = new int[3]; Return_two r = new Return_two(); // Store the result in your custom class instance MethodResult result = r.fun(a, b); // Access the values using getters int[] updatedArray = result.getArrayResult(); int updatedInt = result.getIntResult(); } // Now your method returns the custom MethodResult type public MethodResult fun(int[] array, int var){ // Do your logic here—modify array and var as needed array[0] = 10; var = 20; // Return both values wrapped in MethodResult return new MethodResult(array, var); } }
Why this works: This approach is type-safe, self-documenting, and easy to extend if you ever need to return more values later.
2. Use an Object Array (Quick & Dirty)
If you don't want to make a new class, you can pack both values into an Object[] and return that. Just note that you'll have to cast the values when retrieving them, which makes this less type-safe.
Example Code:
class Return_two{ public static void main(String args[]){ int b=0; int a[] = new int[3]; Return_two r = new Return_two(); // Store result in an Object array Object[] result = r.fun(a, b); // Cast back to the original types int[] updatedArray = (int[]) result[0]; int updatedInt = (int) result[1]; } // Return an Object array containing both values public Object[] fun(int[] array, int var){ // Modify array and var array[1] = 5; var = 15; // Pack into Object array return new Object[]{array, var}; } }
Caveat: This is okay for small, quick scripts, but it's not ideal for larger codebases because other developers won't immediately know what's in the array, and casting can lead to runtime errors if not done carefully.
3. Use a Pair/SimpleEntry (Standard Library Option)
Java's standard library has AbstractMap.SimpleEntry (available since Java 6) which lets you return a pair of values. It's a pre-built way to wrap two values without making your own class.
Example Code:
import java.util.AbstractMap; import java.util.Map; class Return_two{ public static void main(String args[]){ int b=0; int a[] = new int[3]; Return_two r = new Return_two(); // Store result in a SimpleEntry Map.Entry<int[], Integer> result = r.fun(a, b); // Access values with getKey() and getValue() int[] updatedArray = result.getKey(); int updatedInt = result.getValue(); } // Return a SimpleEntry holding the array and integer public Map.Entry<int[], Integer> fun(int[] array, int var){ // Modify your values array[2] = 7; var = 25; return new AbstractMap.SimpleEntry<>(array, var); } }
Note: This is better than the Object array for type safety, but it's still less descriptive than a custom class—someone reading your code might wonder what "key" and "value" represent, whereas a custom class with meaningful getter names makes it obvious.
To answer your original questions:
- Your current
int[]return type isn't suitable because it can only return the array, not the integer variable. - Where to store the result? It depends on the approach you pick: use a custom class instance, an
Object[], or aSimpleEntry—not a regular int variable or single array, since those can only hold one type of value.
内容的提问来源于stack exchange,提问作者user9436026

