Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return multiple values? [duplicate]

Tags:

java

Is it possible to return two or more values from a method to main in Java? If so, how it is possible and if not how can we do?

like image 882
user1089660 Avatar asked Dec 19 '11 06:12

user1089660


People also ask

How do I return multiple values using VLOOKUP in Excel to remove duplicates?

Specify multiple columns VLOOKUP can return a value from a single column, but we can easily return multiple column values with Power Query. To do so, just click the Expand icon on the right side of the Detail column header, or the Transform > Structured Column > Expand command.

How do I get VLOOKUP to return duplicate values?

The VLOOKUP Function always returns the first match. In order to return duplicate values (or the nth match) we need: A new unique identifier to differentiate all duplicate values. A helper column containing a list of unique IDs that will serve as the new lookup column (first column) of the table array.


2 Answers

You can return an object of a Class in Java.

If you are returning more than 1 value that are related, then it makes sense to encapsulate them into a class and then return an object of that class.

If you want to return unrelated values, then you can use Java's built-in container classes like Map, List, Set etc. Check the java.util package's JavaDoc for more details.

like image 55
Aravind Yarram Avatar answered Oct 06 '22 01:10

Aravind Yarram


You can do something like this:

public class Example {     public String name;     public String location;      public String[] getExample()     {         String ar[] = new String[2];         ar[0]= name;         ar[1] =  location;         return ar; //returning two values at once     } } 
like image 32
RanRag Avatar answered Oct 06 '22 01:10

RanRag