0

Possible Duplicate:
How to return multiple objects from a Java method?

lets say N= a+b; for a number N i want to generate the all possible values a and b. like if N =7 a and b are (1+6),(2+5),(3+4).

i have coded this logic in a method.

static void sumofNum(int N){

        for(int a=1; a<N; a++){
                //a+b=N
                int b = N-a;
                System.out.println(a+","+b);
                int next =a+1;
                if(next==b | a==b)
                    return;
        }   
    }

i want to return (1,6),(2,5),(3,4) from this method. next for any N there can be more (a,b) combinations to be returned from this method.

Community
  • 1
  • 1
navyad
  • 3,752
  • 7
  • 47
  • 88

4 Answers4

3

Return a List<String> (assuming "(1,6)" is to be stored as a String). Use one of the implementations of List, such as ArrayList, to construct the list:

static List<String> sumofNum(int N)
{
    List<String> result = new ArrayList<String>();

    for(int a=1; a<N; a++)
    {
        int b = N-a;
        result.add("(" + a + "," + b + ")");
        int next =a+1;
        if(next==b || a==b)
            return result;
    }   
    return result;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Why not to create a new type instead of using **String**? – Roy Ling Sep 29 '12 at 07:48
  • @RoyLing, why do you need to? – hmjd Sep 29 '12 at 07:52
  • 1
    It's easy to read the code and it doesn't need additionally to _split/parse_ the string in the case if you want to get the 2 int results. – Roy Ling Sep 29 '12 at 08:04
  • @RoyLing, where does it state the two `int` results are required? If they are , then introducing a `class` containing two `int`s is straightforward. – hmjd Sep 29 '12 at 08:11
1

If you want to return them as ints, define a object that contains two ints (or abuse points as I have done below) and return a list of those objects. If you define your own object, just replace point with that.

static ArrayList<Point> sumofNum(int N){
    ArrayList<Point> result = new ArrayList<Point>();
    for(int a=1; a<N; a++){
            //a+b=N
            int b = N-a;
        System.out.println(a+","+b);
        int next =a+1;
        if(next==b | a==b)
           result.add(new Point(a,b));
        }   
    return result;
    }

You can get your results from the list with:

results = sumofNum(7);
int a = results.get(0).x; //a = 1
int b = results.get(0).y; //b = 6
Laura
  • 168
  • 2
  • 9
1

In an object oriented (and also functional) style of programming you can pass the result to a consumer an avoid the overhead of storing results in collections or lists.

Example:

static void sumofNum(int N){
  for (int a=1; a<N; a++){
    //a+b=N
    int b = N-a;
    consumer.consume(a,b);
    int next =a+1;
    if (next==b || a==b)
      return;
    }   
}

[ Further improvements of the code are possible (e.g. avoid the inner if and return), ... ]

mmehl
  • 234
  • 1
  • 6
0
Consider the nos as (1,6),(2,5),(3,4)

- Now return an ArrayList<String> which contains each value in String form as "1,6" , "2,5", "3,4".

- When you receive the returned ArrayList, then use split() method "," as delimiter to get the 1 and 6 out of "1,6" and so on....

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75