How can I put a Java array inside itself?

java array methods
arraylist java
java array of objects
array length java
how to set a value in an array java
char array declaration in java
static array java
access array element java

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: 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]) // error: array required, but Object found

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

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> { }

Arrays of Objects | Think Java, The instance variables are private : we can access them from inside this class, but contains references to cards; it does not contain the Card objects themselves. demonstrates the use of a 2D array; specifically, an array of� Enter your email and we will send you instructions on how to reset your password

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

Arrays in Java, In Arrays in Java part of the Java tutorial, we show how to use arrays in Java. Inside the body of the method, a new array is created; it will contain the newly ordered In such an array, the elements are themselves arrays. In Java, the elements of an array can be any type of object you want, including another array. This is called a two-dimensional array — or (sometimes) an array of arrays. Two-dimensional arrays To declare a two-dimensional array, you simply list two sets of empty brackets, like this: int numbers[][]; Here, numbers is a two-dimensional …

Your code is equivalent to

Object arr = theArray[1];  // arr is an Object here, not an array 

But you could do

Object[] arr = (Object[] ) theArray[1];    // Now it is an array

How can I put a Java arrays inside an array?, ExampleLive Demoimport java.util.Arrays; public class ArrayWithinAnArray{ public static void main(String args[]) { int[] myArray1 = {23, 56, 78, An element inside an array can be of any type, and different elements of the same array can be of different types : string, boolean, even objects or other arrays. This means that it’s possible to create an array that has a string in the first position, a number in the second, an object in the third, and so on.

This can be done quite easily with ArrayLists:

ArrayList list = new ArrayList();

So now



System.out.println(((ArrayList)list.get(0)).get(0)); //Casting because .get() returns an Object

Both will output the same thing.

You can take this to an arbitrarily large number of levels, if you want to:


Java Arrays, You can use a Java array as a field, static field, a local variable, array variable you only declare the variable (reference) to the array itself. This example creates an array of type int with space for 10 int variables inside. There are two special kinds of inner classes: local classes and anonymous classes. Shadowing. If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope.

Chapter 10. Arrays, If the component type of an array is T, then the type of the array itself is written T [] . the Java Virtual Machine could not perform the store check described in the� There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular. Thanks to Apache Commons Utils, You can use there ArrayUtils class to remove an element

Day 5 -- Arrays, Conditionals, and Loops, Each of the elements inside the braces must be of the same type and must be the same You'll find yourself running into arrays a lot the more you use Java. The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class. The methods of this class can be used by the class name itself. Class Hierarchy: java.lang.Object ↳ java.util.Arrays

5. Working with Arrays and Loops, You can also create, and use, a literal array in a function or method call: and the third array literals—themselves containing two array members, each with a string literal. alert(multiArray[2]); // prints out test,again,Java,script,read,books� Note that all array subscripts are checked when your Java program is run to make sure that they are inside the boundaries of the array (greater than or equal to 0 but less than the array's length). Unlike in C, it is impossible in Java to access or assign a value to an array slot outside the boundaries of the array (thereby avoiding a lot of

  • Hint: Read up on the Java instanceof operator. You'll need that.
  • You could use ((Object[])((Object[])theArray[1])[1])[0] :)
  • @Eng.Fouad That reminds me of some obfuscation I've seen that collapsed everything into Object[] instances, resulting in wild series of casts; it definitely is a bit obscure at first sight.
  • Right. An element of your Object[] array is, to the compiler, just an Object. It needs to be cast to Object[] using the (Object[]) cast operation. This causes the compiler to generate a runtime check to assure that it really is an Object[] the way you say it is.
  • @HotLicks: Actually, the runtime check comes from the JITter, I believe.
  • @AndersonGreen: That's because (Object[])(theArray[1])[1] is also an Object. Can you understand why?
  • @AndersonGreen You're just getting the error one array further down now (because it doesn't know that theArray[1][1] is an Array...)
  • @LukeWoodward: I never knew that; thanks!…
  • BTW, that will never (AFAIK?) crash.
  • It's the same array; it will never run out. And it won't leak memory or stack, either.
  • @SLaks Why would this not run out? Each iteration sets innerArray to point to its own second element... unless I've done something stupid without noticing?
  • innerArray[1] is innerArray.
  • innerArray takes value of theArray[1]; innerArray takes value of innerArray[1], which is theArray[1], which is innerArray. Thus, the line innerArray = (Object[]) innerArray[1] actually does nothing, and the loop is, in fact, infinite.