Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put a Java array inside itself?

Tags:

java

arrays

I'm attempting to create a Java object array and place the array inside itself at its second index (in order to represent a self-similar fractal with the array), but when I try to access theArray[1][1][0], I get this error:

Main.java:11: error: array required, but Object found.

This is what I've tried so far, and I'm not sure why it's not working:

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Object[] theArray = new Object[2];
        theArray[0] = "This array should contain itself at its second index.";
        theArray[1] = theArray; //Now I'm attempting to put the array into itself.
        System.out.println(theArray[1][1][0]) //Main.java:11: error: array required, but Object found
    }
}

Is it actually possible to put a Java array inside itself, as I'm attempting to do here?

like image 751
Anderson Green Avatar asked May 21 '13 20:05

Anderson Green


People also ask

Can you put an array inside an array Java?

In Programming, an array a linear data structure that can store a fixed-size sequential collection of elements of the same type. We can use arrays to store other arrays also. This way, we create a multi-dimensional array. The sub-arrays can also contain other arrays.

How do you declare an array inside an array?

String[][] arrays = new String[][] { array1, array2, array3, array4, array5 }; (The latter syntax can be used in assignments other than at the point of the variable declaration, whereas the shorter syntax only works with declarations.)

How do you add inside an array?

When you want to add an element to the end of your array, use push(). If you need to add an element to the beginning of your array, try unshift(). And you can add arrays together using concat().


2 Answers

theArray[1] is of compile-time type Object (since it comes from an array of Objects).

You need to cast it to Object[] to use it as an array.


The fundamental problem you're encountering is that although an array that contains itself is a perfectly valid object, it isn't a valid type.

You can nest array types arbitrarily deeply – Object[][][][][][][][][][][][][] is a valid type.
However, the "bottom level" of the type can't be an array.

You're trying to create a type which is an array of itself.
Using generics, that would be possible:

class Evil extends ArrayList<Evil> { }
like image 134
SLaks Avatar answered Sep 22 '22 15:09

SLaks


You're running into a casting error since you've declared theArray to be an Array of Objects. As a result, you can't promise Java that theArray[1] is an Array--it could be any kind of Object. You'll need to break up your access to do what you want:

Object[] innerArray = (Object[]) theArray[1];
System.out.println(innerArray[0] == theArray[0]); // Always true since innerArray IS theArray
while (true) {
    // Careful! This loops forever!
    // set innerArray = innerArray[1] = theArray = theArray[1] = innerArray... 
    // all of these are the exact same object (but you have to tell Java their type every time)
    innerArray = (Object[]) innerArray[1]; 
    System.out.println(innerArray[0]);
}
like image 37
Henry Keiter Avatar answered Sep 21 '22 15:09

Henry Keiter