Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to return multiple values from a function in Java

3 Answers

0 votes
public class ReturnMultipleValuesFromFunction_Java  {
    public static double[] f() {
        double[] point = new double[2];
        
        point[0] = 9078.2;
        point[1] = 3.14;
        
        return point;
    }
    public static void main(String[] args) {
        double point[] = f();

        System.out.println(point[0] + " : " + point[1]);
    }
}


/*
run:

9078.2 : 3.14

*/

 



answered Sep 19, 2024 by avibootz
0 votes
import java.util.ArrayList;
import java.util.List;

public class ReturnMultipleValuesFromFunction_Java {
    public static List<Number> f() {
        List<Number> point = new ArrayList<>();
        
        point.add(283); 
        point.add(109.542); 
        
        return point;
    }
    public static void main(String[] args) {
        List<Number> point = f();

        System.out.println(point);
        
        for (Number item : point) {
            System.out.println(item);
        }
    }
}


/*
run:

[283, 109.542]
283
109.542

*/

 



answered Sep 19, 2024 by avibootz
0 votes
import java.util.HashMap;
import java.util.Map;

public class ReturnMultipleValuesFromFunction_Java {
    public static Map<String, Number> f() {
        Map<String, Number> coordinates = new HashMap<>();
        
        coordinates.put("longitude", 11);
        coordinates.put("latitude", 14.7);
        
        return coordinates;
    }
    public static void main(String[] args) {
        Map<String, Number> coordinates = f();

        System.out.println(coordinates);
        
        for (Map.Entry<String, Number> entry : coordinates.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}


/*
run:

latitude=14.7, longitude=11}
latitude : 14.7
longitude : 11

*/

 



answered Sep 19, 2024 by avibootz

Related questions

3 answers 215 views
1 answer 84 views
4 answers 110 views
4 answers 118 views
1 answer 61 views
1 answer 78 views
...