tomaz108
asked on
returning an array from method
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
I don't know how to return all posible combinations of elements in myArray. In method perRec i get all posible combinations(permutations)
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
ASKER
How can I use global array, myArray is in another class. I'm begginer in Java so I need basic instructions. Thank you.
You can have this big array in your class alog with myArray, and use anywhere you want.
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();
static int[] myNewArray = null;
static public void perRecNew(int n0, int[]a)
{
if(myNewArray == null){
myNewArray = new int[factorial(a.length)][a
}
int zaporedje_indeksov[]=new int [a.length];
myNewArray[n0]=perRec(n0,a
}
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();
ASKER
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...
static public int[][] perRec(int n0, int[]a, int fak)
{
int x = 0;
int zaporedje_indeksov[][]=new
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)
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...
ASKER
can somebody help me?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)][zapo