Java的:通用静态多维数组 [英] Java: Generic Static Multidimensional Arrays

查看:183
本文介绍了Java的:通用静态多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有可能,我怎么能创建一个静态在多维Java数组每个维度不同原始数据类型?

If it is possible, how can I create a static multidimensional array in Java with different primitive datatypes per dimension?

这是静态的,我的意思是原始的数组,它是不是动态就像一个ArrayList会。

By static, I mean the primitive array that is not dynamic like an ArrayList would be.

推荐答案

在一个阵列维度总是从int类型。想想吧!

Dimensions in an Array are always from type int. Think about it!

int a = 4;
int b = 5;

Shoe shoe = new Shoe (Color.RED, 42, "Leather");
Hat hat = new Hat (17, Color.Black);

Foo foo = foos[a][b];
Zilch pop = bars[shoe][hat]; // no go

如果您有FOOS的多维数组,第一维是一个富,FOOS的第二个数组,富的数组的第三个数组。唯一的变量类型是一个在底部。

If you have a multidimensional array of Foos, the first dimension is an Foo, the second an array of Foos, the third an array of array of Foo. The only variable type is the one at the bottom.

数组不叫静态或原始。它们的尺寸是固定的初始化,并且它们有共同的与基元的是,它们是一个buildin的,这在某些情况下,threated特殊。他们是 - 在对比的是所谓的原始类型,这是不是原始的(他们有,例如,运营商,专供自己的,像 * / - )但同时他们的对象,但在库中未声明。

Arrays aren't called static or primitive. Their size is fixed on initialization, and what they have in common with primitives is, that they are a buildin, which is threated special in some cases. They are - in contrast to the so called primitive types, which aren't that primitive (they have, for example, operators, exclusively for their own, like * / -) but meanwhile they are objects, but not declared in the library.

给他们打电话构建类型

使用Bhesh古龙的惯用伎俩:

Using Bhesh Gurung's trick:

Object[] arr = {new Integer[]{}, new String[]{}, new Double[]{}}; 

被找麻烦,这不是由每个维度不同的数据类型。让我们先从尺寸:

is begging for trouble, and it is not made of different datatypes per dimension. Let's start with the dimensions:

// One-dimensional object:
JPanel [] panels = new JPanel [3]; 
// Two-dimensional object:
JPanel [][] panels = new JPanel [3][10]; 

您对底层JPanels和JPanel的上下一维数组。您可以添加更多的维度,总是会得到缠额外的(数组...)。

You have JPanels on the bottom level, and an Array of JPanel on the next dimension. You can add more dimension, and will always get an additional (Array of ...) wrapped around.

您不能如int,char或JPanel并JFrame的阵列混合使用不同的数据类型,或int和JButton的。只有当你在抽象的区别,并使用JPanel并JFrame的共同父的JComponent的,但是这不会对内建类型int,焦炭,布尔等工作,因为它们不是对象。

You can not mix different datatypes in an Array like int and char, or JPanel and JFrame, or int and JButton. Only if you abstract over the difference, and use an JComponent for JPanel and JFrame as common parent, but this will not work for the build-in types int, char, boolean and so on, because they aren't objects.

但你不能使用自动装箱,并使用整数不是int,字符而不是字符,然后使用对象为普通父类?是的,你可以,但你不使用的原语多提了,你乞讨的烦恼。

But can't you use autoboxing, and use Integer instead of int, Character instead of char, and then use Object as common parent class? Yes, you could, but then you're not using the primitives any more, and you're begging for troubles.

丹在谈论不同的事情 - 用不同的充类型的多维数组索引中的:

Dan is talking about a different thing - using differnt types for indexing in the multi-dimensional array:

byte  b = 120;
short s = 1000;
String o [][] = new String[b][s];
b = 7;
s = 9;  
o[b][s] = "foobar";
String foo = o[b][s];

有是用字节或短裤没问题,但你不能用它声明为字节或短的限制数组的大小。在大多数情况下,一个内置的整数类型的边界将不适合于数据类型(认为每年365天),尤其是,因为所有类型的可能得到负值,所以边界检查是必要的,尽管并不能限制编译时间。

There is no problem using bytes or shorts, but you can't restrict the size of an Array by declaring it as byte or short. In most cases the boundaries of a build-in integer type will not fit to a datatype (think 365 days per year), especially, since all types might get negative, so bounds-checking is necessary although and can't be restricted to compile time.

但是,现在的麻烦:结果
我们可以在阵列声明为从一开始二维

But now to the trouble:
We could declare the array as two-dimensional from the beginning:

Object[][] ar2 = {
    new Integer [] {4, 5, 6}, 
    new String [] {"me", "and", "you"}, 
    new Character [] {'x', 'y', 'z'}};

这正常工作,并使得内没有阵列立即铸造访问。但它是唯一已​​知的编译器,该元素是对象数组 - 底层类型是抽象掉,所以我们可以这样写:

That works fine, and makes the inner arrays accessible immediately without casting. But it is only known for the compiler, that the elements are Object arrays - the underlying type is abstracted away, and so we can write:

ar2[1][1] = 17; // expected: String
ar2[2][0] = "double you"; // expected: Char

这将编译完美,但你搬起石头砸自己的脚,并得到一个运行时异常是免费的。

This will compile flawlessly, but you're shooting yourself in the foot and get a Runtime exception for free.

下面是源作为一个整体:

Here is the source as a whole:

public class ArrOfMixedArr
{
    public static void main (String args[])
    {
        Object[] arr = {
            new Integer [] {1, 2, 3}, 
            new String [] {"you", "and", "me"}, 
            new Character [] {'a', 'b', 'c'}};
        show (arr);

        byte b = 7;
        short s = 9;
        String o [][] = new String[200][1000];
        o[b][s] = "foobar";
        String foo = o[b][s];

        Object[][] ar2 = {
            new Integer [] {4, 5, 6}, 
            new String [] {"me", "and", "you"}, 
            new Character [] {'x', 'y', 'z'}};
        show (ar2);

        // exeptions:
        ar2[1][1] = 17; // expected: String
        ar2[2][0] = "double you"; // expected: Char
    }

    public static void show (Object[] arr)
    {
        for (Object o : arr) 
        {
            if (o instanceof Object[])
                show ((Object[]) o);
            else 
                System.out.print (o.toString () + "\t");
        }
        System.out.println ();
    }
}

现在该如何解决?
如果(INT,字节,字符,字符串的JPanel,...)长度相等,那么你必须像一个隐藏的对象,一个数据库行的基本类型的数组。使用一个类来代替:

Now what is the solution? If your base-types arrays of (int, byte, char, String, JPanel, ...) are of equal length, then you have something like a hidden Object, a database-row. Use a class instead:

class Shoe {
    byte size;
    String manufactor;
    java.math.BigDecimal price;
    java.awt.Color color;
}

Shoe [] shoes = new Shoe [7];

如果您没有不同类型的大小相同,它们可能是不相关的,并且不应当被放置在一个共同的容器中。

If you don't have different types of the same size, they might be unrelated, and should not be put in a common container.

这篇关于Java的:通用静态多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆