Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

returning an array from method

Posted on 2005-04-02
7
Medium Priority
?
183 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

571 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