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.

0

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why not to create a new type instead of using String?
@RoyLing, why do you need to?
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.
@RoyLing, where does it state the two int results are required? If they are , then introducing a class containing two ints is straightforward.
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

Comments

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), ... ]

Comments

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....

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.