Dhiraj
Dhiraj

Reputation: 910

How can I declare an "infinite" 2D array in Java?

I want an infinite 2D array in my program. I have the following code for a 2D array:

int a[][]=new int[10][10];

But the problem is that the array is of fixed length, with row and column values.

Upvotes: 1

Views: 5333

Answers (4)

Jordi Castilla
Jordi Castilla

Reputation: 26961

As Kayaman commented, arrays in java are fixed size as specified in JLS (§10.3)

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.

You have 3 alternatives, if first is not possible in your case, you should use 2nd one:

  1. use a variable to give array extact size you need:

    int numberOfXElement = // get the elements in dynamic way
    int numberOfYElement = // get the elements in dynamic way
    int a[][]=new int[numberOfXElement][numberOfYElement];
    

BEST OPTION

  1. use ArrayList which has dynamic size and you can add elements without caring the size (but not until infinite, just till Integer.MAX_VALUE).

    List<List<Integer>> yourList = new ArrayList<ArrayList<Integer>>();
    

    NOTE: as pointed on comments, take care you are using Integer and not int.


  1. give Integer.MAX_VALUE size to array (but must be careful with empty positions)
    as pointed in comments, syntactically is a correct expression, but you need tones of ram memory to execute it. Instead of this:

    give a big number you will never reach to array when creating, but if you use this option you must be careful with empty positions.

    int a[][]=new int[8043][8043];
    

    Why 8043? I used a quick test to see max heap size in my actual machine, 2gb RAM, does not care which value i starts, always breaks (in my machine, trying to create an int[8044][8044]).

    for (int i = 0; ; i++) {
        int a[][]=new int[i][i];
        System.out.println(i + " " + Arrays.toString(a));
    }
    

Upvotes: 4

OldCurmudgeon
OldCurmudgeon

Reputation: 65821

Declare yourself a class Point with equals and hashCode - something like this:

    class Point {

        private final int x;
        private final int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 79 * hash + this.x;
            hash = 79 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

    }

Then use a:

Map<Point,Value> twoDArray= new HashMap<>();

Setting a particular location to a value would be as simple as:

twoDArray.put(new Point(x,y), value);

If your array is likely to be sparse then there are meny specialst classes that can help.

Upvotes: 1

Viktor Mellgren
Viktor Mellgren

Reputation: 4506

Not sure if it's possible to create a stream of streams, but in Java 8 you can have infinite streams.

https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#generate-java.util.function.IntSupplier-

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Arrays are fixed in size. Once you declare them with some length, you cannot change it later.

You might want to have an ArrayList which increase size dynamically while you keep adding the items.

If you are looking for 2D, ArrayList of ArrayList will be the option.

List<List<Integer>> listOfLists = new ArrayList<ArrayList<Integer>>();

And if you don't exactly know why I wrote List<List<>> instead of ArrayList<ArrayList>> on left side, please refer Type List vs type ArrayList in Java

Upvotes: 3

Related Questions