简体   繁体   中英

creating an array inside a while loop(or for loop) then using that array outside

What I am trying to do, which this won't do and probably never will do, is to create an array using a loop and then calling that array in another class. What would be the best way to go about this. I've looked all over for a solution and they won't compile. Thanks for the help.

import java.util.*;
public class Test{

public static void main(String[] args){ 

    Grow t = new Grow();
    ArrayList arr = new ArrayList();
    arr = t.Grow();
    System.out.print(arr);

}
}

public class Grow{
    static int row;
    static int column;

public ArrayList<Integer> poli(){
        while(row < 51 ){
            int r = row;
                while(column < 1){
                    ArrayList<Integer> policy = new ArrayList<Integer>();
                    policy.add(r);
                    policy.add(0);

                }

            column-=1;
            row++;
        }

    return policy;

}

EDIT: new problem. Getting error Exception in thread "main"java.lang.OutOfMemoryError:Java heap space at java.util.Arrays.copyOf(Arrays.java:3210) at java.util.Arrays.copyOf(Arrays.java:3181) at java.util.ArrayList.grow(ArrayList.java:261) at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235) at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227) at java.util.ArrayList.add(ArrayList.java:458) at Grow.policy(Grow.java:14) at Test.main(Test.java:8)

public class Test{
public static void main(String[] args){ 

Grow trr = new Grow();
ArrayList arr = trr.policy();
System.out.print(arr);

}
}

Here is what I have now. Everything works until I try to make the array in Test class.

import java.util.*;

public class Grow{
    static int row;
    static int column;

public ArrayList<Integer> policy(){

ArrayList<Integer> policy = new ArrayList<Integer>();
    while(row < 51 ){
          while(column < 1){

                policy.add(row);
                policy.add(0);
                column++; //this addition fixed my problem
            }

        column-=1;
        row++;
    }

return policy;

    }   
}

public class Test{

    public static void main(String[] args){ 
       Grow work= new Grow();
       ArrayList testing= work.policy();
       System.out.print(testing);

    }
}

I know it's something simple. I just can't see the solution. Thanks in advance. EDIT: Update. Found the problem. Column only had a decrease of 1 (really could use column--;) which made it loop forever. Added column++ to the code and now it works. Knew it was something simple.

You should initialize it outside:

public ArrayList<Integer> poli(){

    ArrayList<Integer> policy = new ArrayList<Integer>();

    while(row < 51 ){
        int r = row;
            while(column < 1){

                policy.add(r);
                policy.add(0);

            }

        column-=1;
        row++;
    }

return policy;

}

Also, you don't need a variable to store row . policy.add(row); will work fine too.

Found the problem. Column only had a decrease of 1 (really could use column--;) which made it loop forever. Added column++ to the code and now it works. Knew it was something simple. Working code

import java.util.*;

public class Grow{
    static int row;
    static int column;

public ArrayList<Integer> policy(){

ArrayList<Integer> policy = new ArrayList<Integer>();
    while(row < 51 ){
       while(column < 1){

            policy.add(row);
            policy.add(0);
            column++; //this addition fixed my problem
        }

        column-=1;
        row++;
     }

    return policy;

    }   
}

public class Test{

    public static void main(String[] args){ 
       Grow work= new Grow();
       ArrayList testing= work.policy();
       System.out.print(testing);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM