我怎样才能把一个Java数组里面本身?

编程入门 行业动态 更新时间:2024-10-10 08:19:49
本文介绍了我怎样才能把一个Java数组里面本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图创建一个Java对象阵列和阵列置于内本身在其第二个索引(以重present自相似的分形与阵列),但是当我尝试访问 theArray [1] [1] [0] ,我得到这个错误:

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:错误:所需的数组,但对象中找到

这是我到目前为止已经试过了,我不知道为什么它不工作:

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

时,它实际上可能把Java数组里面本身,我试图在这里做什么?

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

推荐答案

theArray [1] 是编译时间的类型对象(因为它来源于对象的数组)。

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.

您可以嵌套数组类型任意深深&ndash的; 对象[] [] [] [] [] [] [] [] [] [] [] [] [] 是一个有效的类型。结果然而,该类型的底层不能是一个数组。

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

更多推荐

我怎样才能把一个Java数组里面本身?

本文发布于:2023-11-29 13:30:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!