?
Solved

returning an array from method

Posted on 2005-04-02
7
Medium Priority
?
182 Views
Last Modified: 2010-03-31
I have a recursive method for geting all permutations of my array. Method is recursive because I need it like this.
I don't know how to return all posible combinations of elements in myArray. In method perRec i get all posible combinations(permutations) of indexes. I want to return this combinations and execute calculation (for elements in myArray) for each combination;

 METHOD:

 static public int[] perRec(int n0, int[]a)
         {
       int zaporedje_indeksov[]=new int [a.length];                  
         if(n0==0){
                      for ( int j=0; j<a.length ;j++) {
                       zaporedje_indeksov[j] = a[j];
                                   }
                      return zaporedje_indeksov;
                           }
         else
               {
          for(int i=0, temp; i < n0; i++) {
          temp = a[i]; a[i] = a[n0-1]; a[n0-1] = temp;
          perRec(n0-1, a);
          temp = a[i]; a[i] = a[n0-1]; a[n0-1] = temp;
                                                }
               }
       return zaporedje_indeksov;}
       
       
  CALLING OF METHOD:
 
      for (int i=0; i<myArray.length;i++)
           tabelaIndeksov[i]=i;      // initialization

             Podatki.perRec(dolzina, tabelaIndeksov);
      // here I must get all combinations of values in MyArray and for each combination   execute calculation

0
Comment
Question by:tomaz108
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13687961
If You can, do Your subsequent calculations inside perRec function

static public int[] perRec(int n0, int[]a)
        {
       int zaporedje_indeksov[]=new int [a.length];              
        if(n0==0){
                    for ( int j=0; j<a.length ;j++) {
                     zaporedje_indeksov[j] = a[j];
                               }
                    return zaporedje_indeksov;  <-- instead of this return statement perform needed calculations
                       }
        else
             {...

or use global array to store each zaporedje_indeksov array instead of returning it,
the array size should be [factorial(a.length)][zaporedje_indeksov.length], also You'll have to write function 'factorial'
0
 

Author Comment

by:tomaz108
ID: 13688067
How can I use global array, myArray is in another class. I'm begginer in Java so I need basic instructions. Thank you.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13688672
You can have this big array in your class alog with myArray, and use anywhere you want.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:bhaskar20001
ID: 13688815
Try like this..I didn't tested it..

static int[] myNewArray = null;
static public void perRecNew(int n0, int[]a)
{    
   if(myNewArray == null){
     myNewArray = new int[factorial(a.length)][a.length];
   }
   int zaporedje_indeksov[]=new int [a.length];  
   myNewArray[n0]=perRec(n0,a,zaporedje_indeksov);
}

static public int[] perRec(int n0, int[] a,int[] zaporedje_indeksov)
 {
        if(n0==0){
                    for ( int j=0; j<a.length ;j++) {
                     zaporedje_indeksov[j] = a[j];
                               }
                    return zaporedje_indeksov;
                       }
        else
             {
         for(int i=0, temp; i < n0; i++) {
         temp = a[i]; a[i] = a[n0-1]; a[n0-1] = temp;
         perRec(n0-1, a);
         temp = a[i]; a[i] = a[n0-1]; a[n0-1] = temp;
                                        }
             }
       return zaporedje_indeksov;
  }
       
 public int[] getMyPerCom()
{
   return myNewArray;
}

----------------------------------
CALLING OF METHOD:
 
      for (int i=0; i<myArray.length;i++)
      {
           tabelaIndeksov[i]=i;     // initialization
            Podatki.perRec(dolzina, tabelaIndeksov);
      }
      int[] perCom = Podatki.getMyPerCom();
     

0
 

Author Comment

by:tomaz108
ID: 13691437
I'm trying to do like this:

 static public int[][] perRec(int n0, int[]a, int fak)
       {
   int x = 0;
   int zaporedje_indeksov[][]=new int [a.length][fak];
 
       if(n0==0){
           for ( int j=0; j<a.length ;j++) {
                    zaporedje_indeksov[j][x] = a[j];
                         }
           x++;
                      }
       else
            {
        for(int i=0, temp; i < n0; i++) {
        temp = a[i]; a[i] = a[n0-1]; a[n0-1] = temp;
        perRec(n0-1, a, fak);
        temp = a[i]; a[i] = a[n0-1]; a[n0-1] = temp;
                                       }
            }
        return zaporedje_indeksov;
        }



       int fak = Podatki.fakulteta(dolzina);      // fakulteta  returns faculty of dolzina
 
       for (int i=0; i<tabela.length;i++)      // initialization
           tabelaIndeksov[i]=i;           
       
      for (int j=0; j<fak;j++)       // test
           for (int i=0; i<tabela.length;i++)
               System.out.println (Podatki.perRec(dolzina, tabelaIndeksov, fak)[i][j]);
 
But I get only zero: 0 0 0 0 0 0...
0
 

Author Comment

by:tomaz108
ID: 13692196
can somebody help me?
0
 
LVL 4

Accepted Solution

by:
bhaskar20001 earned 200 total points
ID: 13697407
Small correction in my previous comment..

U change according to ur requirement..

CALLING OF METHOD:
 
      for (int i=0; i<myArray.length;i++)
      {
           tabelaIndeksov[i]=i;     // initialization
            Podatki.perRecNew(dolzina, tabelaIndeksov);
      }
      int[] perCom = Podatki.getMyPerCom();
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question