0

I have to make an array. Unfortunately I cannot figure something out. My problem: I want to make an array (two dimensional) and declare the size of the array in a method. BUT, here is my real problem, I want to use that array in other methods.

I tried to make an array in the class itself but then it is already declared and I cannot change it anymore.

This is the thing: I need an array whose size I can set myself. After that I want another method which can calculate something in an array. I also need another method to do something else in that array.

So my question is: How do I declare an array and make use of it in other methods? My code is on the bottom.

Hopefully it is explained good enough for you guys to help me.

public class Operations
{

    public final int SET = 1;
    int rows;
    int cols;
    int objects;
    int times;
    int doubles = 0;
    int empty = 0;
    int world[][] = new int[rows][cols];

    public void print()
    {
        System.out.println(rows);
        System.out.println(cols);
        System.out.println(objects);
        System.out.println(times);
    }

    public void setParameters(int rows, int cols, int objects, int times)
    {
        this.rows = rows;
        this.cols = cols;
        this.objects = objects;
        this.times = times;
    }

    public void addObjects()
    {

        Random random = new Random();

        for (int i = 0; i < objects; i++)
        {
            int randomx = random.nextInt(rows);
            int randomy = random.nextInt(cols);

            world[randomx][randomy] += SET;
        }

    }

}
2
  • When you call setParameters() you need to update the size of world since rows and cols are not instantiated when you first declare world. In setParameters add world = new int[rows][cols]; and change the initial declaration to int world[][]; Commented Sep 27, 2013 at 14:13
  • Do you need to access the array inside the class or do you want to access it in a method in another class ? Commented Sep 27, 2013 at 14:19

3 Answers 3

2

You should make a constructor instead of using the setParameters method. You can make the constructor like this:

public Operations(int rows, int cols, int objects, int times)
{
    this.rows = rows;
    this.cols = cols;
    this.objects = objects;
    this.times = times;
    world = new int[rows][cols];
}

The field int world[][] will be initialized in the constructor now. This way you can declare an array and it's size at the same time in other methods. To initialize an object in another method, you would say:

Operations array = new Operations(num_rows, num_cols, num_objects, num_times);
Sign up to request clarification or add additional context in comments.

3 Comments

This worked for me, tanks a lot. That I did not come up with this idea myself :)
Just to point out world[][] = new int[rows][cols]; will not compile. The correct syntax to initialize the array is world = new int[rows][cols];. Working Example with constructor and a method to reinitialize the array.
@AnthonyAccioly you are right. I copied and pasted without cleaning up
2

I tried to make an array in the class itself but then it is already declared and I cannot change it anymore. (...) How do I declare an array and make use of it in other methods?

If i understood your question correctly, you want to declare an array in the class scope and initialize / mutate it through a method... So that other methods can consume the array.

public class Operations
{
   // declare the array
   private int[][] world;

   public void setParameters(int rows, int cols, int objects, int times)
   {
      this.rows = rows;
      this.cols = cols;
      this.objects = objects;
      this.times = times;
      // initialize the array
      world = new int[rows][cols];
   }

   /// .... methods that use the array
}

If you don't want to allow new initializations of the array (while still being able to change their values), follow @fvrghl advice and provide a constructor. Also make your array final so that it can only be initialized once.

public class Operations
{
   // declare the array, note the final keyword
   private final int[][] world;

   public Operations(int rows, int cols, int objects, int times)
   {
      this.rows = rows;
      this.cols = cols;
      this.objects = objects;
      this.times = times;
      world = new int[rows][cols]; 
      // array is initialized, its dimensions are now fixed.
   }

   /// .... methods that use the array

}

5 Comments

Is he really needing the array in a method inside the class or is the question on how to access the array from a method in another class ?
My question is how to use an array in different methods. I want to declare the array and it's size in a method and use it in all my other methods.
Of the same class ? If yes, AnthonyAccioly gave you the answer. If you need to access it outside of the class, simply add the getters and setters to the code given by AnthonyAccioly
Then, as of my understanding you need the first option. world will be initialized by setParameter. All other methods of Operation will be able to use world. Is that what you are looking for?
As he validated another answer it is still not clear what is really needed by the OP.
0

Dont initialize the array here if the array size is dynamic.

int world[][] = null;   

initialize the array with in you setParameters method

public void setParameters(int rows, int cols, int objects, int times)
{
    this.rows = rows;
    this.cols = cols;
    this.objects = objects;
    this.times = times;
    world = new int[rows][cols]; //You need to initialize the array here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.