Android:在一个多维数组中存储 int 和 String 值

发布于 2024-12-05 07:26:36 字数 253 浏览 4 评论 0原文

是否可以将不同的值存储到多维数组中,例如 int 和 String ?

String[][] mainArray= new String[2][2];

    mainArray[0][0] = 1;
    mainArray[0][1] = "Name1";
    mainArray[1][0] = 2;
    mainArray[1][1] = "Name2";

这显然不起作用,因为 1 和 2 不是字符串值

is it possible to store different values into a multidimensional array such as int's and String's?

String[][] mainArray= new String[2][2];

    mainArray[0][0] = 1;
    mainArray[0][1] = "Name1";
    mainArray[1][0] = 2;
    mainArray[1][1] = "Name2";

this obviously doesn't work because 1 and 2 are not String values

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉溺在你眼里的海 2024-12-12 07:26:36

是的,你可以存储
试试这个

        String[][] mainArray= new String[2][2];

        mainArray[0][0] = String.valueOf(1);
        mainArray[0][1] = "Name1";
        mainArray[1][0] = String.valueOf(2);
        mainArray[1][1] = "Name2";

yes you can store
try this

        String[][] mainArray= new String[2][2];

        mainArray[0][0] = String.valueOf(1);
        mainArray[0][1] = "Name1";
        mainArray[1][0] = String.valueOf(2);
        mainArray[1][1] = "Name2";
再可℃爱ぅ一点好了 2024-12-12 07:26:36

您可以创建一个Object数组,并保存Integer,它是基元int的装箱。

Object[][] arr = new Object[2][2];
arr[0][0] = "hello";
arr[0][1] = Integer.valueOf(1);
arr[1][0] = Integer.valueOf(2);
arr[1][1] = "world";

You can create an Object array, and save Integers, which is the boxing of the primitive int.

Object[][] arr = new Object[2][2];
arr[0][0] = "hello";
arr[0][1] = Integer.valueOf(1);
arr[1][0] = Integer.valueOf(2);
arr[1][1] = "world";
海螺姑娘 2024-12-12 07:26:36

这是解决方案

Object[][] arr=new Object[anysize][]; and you can do like this
arr[0][0]=1;
arr[1][0]="hello";

,但是在访问此数组时,您还应该仅使用 Object 来执行此操作。否则可能会出现 ClassCastException。

Here is the solution

Object[][] arr=new Object[anysize][]; and you can do like this
arr[0][0]=1;
arr[1][0]="hello";

But while accessing this array you should also do this using Object only.Else there may be ClassCastException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文