Returnin 3d array problem
Hello! My problem is that i created a class where i did a method and this method returning with a 3d array. When i wud like to use this array in an other class suddenly its a empty array with full of zero.
If someone could help me out i'd appriciate it thx.
Here's the code:
Code :
public class BetarEgy extends Raktar{
BetarEgy(){
super.Randomallapot();
}
public int[][][] betar1(int textfield){
int tomb[][][]=new int[4][25][2];
boolean betarolt=false;
for(int k=0;k<textfield;k++){
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
if(raktar[i][j][0]==0 && raktar[i][j][1]==0 && raktar[i][j][2]==0){
raktar[i][j][0]=1 + gen.nextInt(25)+(i*4);
raktar[i][j][1]=gen.nextInt(2);
raktar[i][j][2]=gen.nextInt(8)+1;
tomb[i][j][0]=i;
tomb[i][j][1]=j;
System.out.println(" Sor: "+tomb[i][j][0]+" Oszlop: "+tomb[i][j][1]);
}
}
}
}
return tomb;
}
}
Above i checked with a print method thatthe array does contain the numbers what i need and it does
Code :
public class Fifo extends BetarEgy{
private final BetarKetto os= new BetarKetto();
// private final BetarEgy t= new BetarEgy();
boolean kitarolt=false;
final int kitartomb[][][]=betar1(textfield); //This is the array what i actually wud like to use
double ido[]= new double[100];
double szamol;
public double[] FifoKitar1(int textfield){
for(int k=0; k<textfield;k++){
for(int i=0;i<3;i++){
for (int j=0;j<24;j++){
if(i==kitartomb[i][j][0] && j==kitartomb[i][j][1]){
System.out.println(" SorFifo: "+kitartomb[i][j][0]+" OszlopFifo: "+kitartomb[i][j][1]);
tav=((Integer)raktar[i][j][0]).intValue();
szamol=tav/(double)seb;
ido[k] = szamol;
//System.out.print(" Idő: "+ido[k]+"\nTavolsag: "+tav+" Raktaradattav: "+raktar[i][j][0]);
raktar[i][j][0]=0;
raktar[i][j][1]=0;
raktar[i][j][2]=0;
kitarolt=true;
break;
}
}
if(kitarolt==true){
break;
}
}
}
return ido;
}
Thx for take a look at it :)
Re: Returnin 3d array problem
When an array of type int is created each element's default value is zero (0). This tells me somehow you are probably passing a new, default value array, rather than your initialized array, but the relevant code is not posted. Look back over your code and verify that the array you are passing is the one initialized with your desired values. If you still have problems finding the error post the code which includes the array and the code where the array is passed to the other class etc.
Re: Returnin 3d array problem
So heres the class that the first class extends and i checked the array its actually gives the numbers i'd like to
i was spending a lot of time front of the code and try to figure out what is the prob but i couldnt.
So its actuallly a warehouse and gives a random status of the warehouse storing.
If an array element zero then there isnt stored yet and if has there stored and this gives the numbers well
so the First Class i've posted of the two in the first that is a storing algorithm so wheres an empty place close it will
load it and the 3d array actually wud contain these elements what have been stored with the algorythm(by tested it works well in the algorythm, gives the numbers of the ranges and collumns) so with the second algorythm
what is first in first out algorythm used the informations i cud take em out and calculate a time of it and store it an other array.
But i cant find the problem where the code has its problem that the array will full of zero when i wud use the array in an other class than it a method generated.
Code :
import java.util.Random;
public class Raktar {
Object raktar[][][] = new Integer[4][25][3];
Random gen = new Random();
int tav;
int mely;
int mag;
int textfield;
public static int seb= 5;
public void Randomallapot(){
int alapszam= gen.nextInt(60)+41;
int szamlal=20;
int tomb[][][] = new int[4][25][2];
int max=alapszam-szamlal;
System.out.println("Maxszam: "+max);
int sorszamlal=0;
int oszlopszamlal=0;
for (int k=0;k<max;k++){
int sor=gen.nextInt(4);
int oszlop=gen.nextInt(25);
boolean volt=false;
for (int i=0; i<4; i++){
for (int j=0; j<25; j++){
if(i==sor && j==oszlop){
if(tomb[i][j][0]!=sor || tomb[i][j][1]!=oszlop && volt==false){
sorszamlal=i;
oszlopszamlal=j;
tomb[sorszamlal][oszlopszamlal][0]=sor;
tomb[sorszamlal][oszlopszamlal][1]=oszlop;
}
else if(tomb[i][j][0]==sor && tomb[i][j][1]==oszlop){
volt=true;
break;
}
}
}
}
for (int i=0; i<4; i++){
for (int j=0; j<25; j++){
if(i==tomb[sorszamlal][oszlopszamlal][0] && j==tomb[sorszamlal][oszlopszamlal][1]){
raktar[i][j][0]=1 + gen.nextInt(25)+(i*4);
raktar[i][j][1]=gen.nextInt(2);
raktar[i][j][2]=gen.nextInt(8)+1;
}else if(raktar[i][j][0]==null && raktar[i][j][1]==null && raktar[i][j][2]==null){
raktar[i][j][0]=0;
raktar[i][j][1]=0;
raktar[i][j][2]=0;
}
}
}
}
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
if (raktar[i][j][0]==null && raktar[i][j][1]==null && raktar[i][j][2]==null ){
raktar[i][j][0]=0;
raktar[i][j][1]=0;
raktar[i][j][2]=0;
}
}
}
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
System.out.print("["+raktar[i][j][0]+" "+raktar[i][j][1]+" "+raktar[i][j][2]+"] ");
}
System.out.println();
}
}
}
Thx for helping.
Re: Returnin 3d array problem
Looking through the code a million times i recognized when im running the code its works almost fine the Raktar class Randomallapot method makes a random status of a warehouse and after the BetarEgy class betar1 method storing into this warehouse, however when the programm goin further to the Fifo class then the Raktar class Randomallapot method runs again generating a new random status of the warehouse and the array what should contain the information of the stored items by betar1 method filled full of zero by default. So pls can u help me find why is it running 2 times instead of one and why the second times is it gets default zero elements int the "tomb" array(contains the range collumn information stored by betar1 method) when go further to the Fifo class.
im posting the the code again because i made some small fix in it but it isnt affect the the array problem. I put the classes in sequence by extends.
Code :
import java.util.Random;
public class Raktar {
public final Object raktar[][][] = new Integer[4][25][3];
Random gen = new Random();
int tav;
int mely;
int mag;
int textfield;
public static int seb= 5;
public void Randomallapot(){
int alapszam= gen.nextInt(60)+41;
int szamlal=20;
int tomb[][][] = new int[4][25][2];
int max=alapszam-szamlal;
System.out.println("Maxszam: "+max);
int sorszamlal=0;
int oszlopszamlal=0;
for (int k=0;k<(max+10);k++){
int sor=gen.nextInt(4);
int oszlop=gen.nextInt(25);
boolean volt=false;
for (int i=0; i<4; i++){
for (int j=0; j<25; j++){
if(i==sor && j==oszlop){
if(tomb[i][j][0]!=sor || tomb[i][j][1]!=oszlop && volt==false){
sorszamlal=i;
oszlopszamlal=j;
tomb[sorszamlal][oszlopszamlal][0]=sor;
tomb[sorszamlal][oszlopszamlal][1]=oszlop;
}
else if(tomb[i][j][0]==sor && tomb[i][j][1]==oszlop){
volt=true;
break;
}
}
}
}
for (int i=0; i<4; i++){
for (int j=0; j<25; j++){
if(i==tomb[sorszamlal][oszlopszamlal][0] && j==tomb[sorszamlal][oszlopszamlal][1]){
raktar[i][j][0]=1 + gen.nextInt(25)+(i*4);
raktar[i][j][1]=gen.nextInt(2);
raktar[i][j][2]=gen.nextInt(8)+1;
}else if(raktar[i][j][0]==null && raktar[i][j][1]==null && raktar[i][j][2]==null){
raktar[i][j][0]=0;
raktar[i][j][1]=0;
raktar[i][j][2]=0;
}
}
}
}
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
if (raktar[i][j][0]==null && raktar[i][j][1]==null && raktar[i][j][2]==null ){
raktar[i][j][0]=0;
raktar[i][j][1]=0;
raktar[i][j][2]=0;
}
}
}
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
System.out.print("["+raktar[i][j][0]+" "+raktar[i][j][1]+" "+raktar[i][j][2]+"] ");
}
System.out.println();
}
}
}
Code :
public class BetarEgy extends Raktar{
BetarEgy(){
super.Randomallapot();
}
int szam;
public final int tomb[][][]=new int[4][25][2];
public Object[][][] betar1(int textfield){
this.szam=textfield;
int szamlalo=0;
System.out.println("Textfield: "+textfield);
System.out.println("Betar raktar: ");
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
if (szamlalo<textfield){
if(raktar[i][j][0]==0 && raktar[i][j][1]==0 && raktar[i][j][2]==0){
raktar[i][j][0]=1 + gen.nextInt(25)+(i*4);
raktar[i][j][1]=gen.nextInt(2);
raktar[i][j][2]=gen.nextInt(8)+1;
tomb[i][j][0]=i;
tomb[i][j][1]=j;
System.out.print("["+raktar[i][j][0]+" "+raktar[i][j][1]+" "+raktar[i][j][2]+"] ");
szamlalo++;
}
}else{
break;
}
}
if(szamlalo==textfield){
break;
}else{
continue;
}
}
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
System.out.println("\n Range: "+tomb[i][j][0]+" Collumn: "+tomb[i][j][1]);
}
}
System.out.println("Final stored array: ");
for (int i=0;i<4;i++){
for(int j=0;j<25;j++){
System.out.print("["+raktar[i][j][0]+" "+raktar[i][j][1]+" "+raktar[i][j][2]+"] ");
}
System.out.println();
}
return raktar;
}
public int getTextfield(){
return textfield;
}
}
This is where the problem starts because i can't get any data of the BetarEgy class everything gets default here
Code :
public class Fifo extends BetarEgy{
boolean kitarolt=false;
double ido[]= new double[100];
double szamol;
public double[] FifoKitar1(){
for(int k=0; k<50;k++){
for(int i=0;i<3;i++){
for (int j=0;j<24;j++){
if(tomb[i][j][0]==i && tomb[i][j][1]==j){
System.out.println(" FifoRange: "+tomb[i][j][0]+" FifoCollumn: "+tomb[i][j][1]);
tav=((Integer)raktar[i][j][0]).intValue();
szamol=tav/(double)seb;
ido[k] = szamol;
raktar[i][j][0]=0;
raktar[i][j][1]=0;
raktar[i][j][2]=0;
kitarolt=true;
break;
}
}
if(kitarolt==true){
break;
}
}
}
return ido;
}
}
Thank in advance for any help :)