简体   繁体   English

抽象数据类型:对象数组

[英]Abstract Data Type: Array of objects

So I have this class, and I want to method that takes in an int, and creates a new array of that size. 因此,我有这个类,并且我想使用一个int方法,并创建一个具有该大小的新数组。 If I declare 如果我声明

newArray<Object<int,int>> array1 = new newArray<Object<int,int>>(10);

This would create an array of size 10. 这将创建一个大小为10的数组。


I've tried doing 我试着做

    public class newArray<O>
    {

        private O[] array;

        public newArray(int newArraySize) 
          {
             newArray<O>[] = new newArray<O>[newArraySize];
          }
}

but I get an " Cannot create a generic array of newArray " error. 但出现“无法创建newArray的通用数组”错误。

Unfortunately, you can't create a generic array. 不幸的是,您无法创建通用数组。 This is due to the way generics are implemented in Java, which is through type erasure. 这是由于Java通过类型擦除实现了泛型的方式。 In effect, all generic types are "erased" to their bounding type (usually Object ) before compilation, so all the compiler sees are Object instead of T or E or O . 实际上,所有通用类型在编译之前都会被“擦除”为其边界类型(通常是Object ),因此编译器看到的都是Object而不是TEO The erasure process generates automatic casts to ensure that the program would still work as intended. 擦除过程会自动生成强制转换,以确保程序仍能按预期运行。

This means that you can't do: 这意味着您不能执行以下操作:

  • new O() (this gets erased to new Object() , which the compiler has no idea what to do with) new O() (将其擦除为new Object() ,编译器不知道该如何处理)
  • new O[] (this gets erased to new Object() , which again isn't helpful to the compiler) new O[] (将其擦除到new Object() ,这再次对编译器没有帮助)

What you can do is casting: 可以做的是投射:

array = (O[]) new Object[size];

And in fact, that's how it's done in Java's Collections framework. 实际上,这就是在Java的Collections框架中完成的。 You'll get an "unchecked" warning, as the compiler can't prove that this conversion is safe, but there really is no other option. 您将收到“未经检查”的警告,因为编译器无法证明此转换是安全的,但实际上没有其他选择。

Also, few things I want to point out about your question, that you may or may not know already: 另外,关于您的问题,我想指出的几件事,您可能已经知道或可能不知道:

  • You can't use primitives as type parameters 您不能将原语用作类型参数
  • Object has no type parameters Object没有类型参数
  • newArray<O>[] = new newArray<O>[newArraySize]; should really be array = new newArray<O>[newArraySize]; 应该确实是array = new newArray<O>[newArraySize]; . You already declared the array you wanted to use. 您已经声明了要使用的数组。 So use it! 所以用吧!

Looks like you're implementing your own ArrayList , in fact. 看起来您实际上正在实现自己的ArrayList If you are, good luck! 如果是,祝您好运! If you aren't, you should really be using the existing implementation, unless you need some special behavior that you can't get otherwise... 如果不是,那么您应该确实使用现有的实现,除非您需要一些其他无法避免的特殊行为...

You are almost there. 你快到了。

Take a look at the following code for an example on how to initialize, populate, and print an array. 请看以下代码,以获取有关如何初始化,填充和打印数组的示例。

import java.util.*;

public class Test 
{
    public static void main(String[] args)
    {
        // define the array
        List<Integer> array1 = new ArrayList<Integer>(10);

        // initialize the array with some value. In this case, the Integer "200"
        // note that the array doesn't have a hard limit of 10 as define above. You can change the following value to 20 and it will still work
        for (int i = 0; i < 10; i++)
        {
            array1.add(i, 200);
        }

        // print the array
        System.out.println(array1);
    }
}

Does this help? 这有帮助吗?

    ArrayList<Integer[][]> r = new ArrayList<Integer[][]>(10);
    // You could replace the above list with a custom list

    Integer[][] ob = new Integer[1][2];
    ob[0][0] = 10;
    ob[0][1] = 20;

    r.add(ob);


    for(Integer[][] o : r)
        for(Integer[] o1 : o)
            for(Integer o2 : o1)
                System.out.println(o2);

And the output is: 输出为:

10
20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM