0

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.

4
  • You can pass around Lists like any other reference. It's not clear what the issue is. Commented Jun 8, 2017 at 18:30
  • The JLS will be one of your best friends. In this case, review the rules on variable scope - docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.3 Specifically : The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement. Commented Jun 8, 2017 at 18:57
  • When are row and col ever initialized? Commented Jun 8, 2017 at 19:43
  • static int initializes them for the entire class so they become a class variable. Default for Int will always be zero @Tezra Commented Jun 12, 2017 at 11:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

maybe you can use List<Integer> policy = new ArrayList<>(); instead of ArrayList<Integer> policy = new ArrayList<Integer>();
That worked beautifully. Can't believe I missed that. Now I have another problem though. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
0

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);

    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.