代码之家  ›  专栏  ›  技术社区  ›  Ayush Kacholiya

无法使用toArray()方法将ArrayList转换为Java中的数组

  •  0
  • Ayush Kacholiya  · 技术社区  · 2 年前

    我正在字符串中使用递归将N打印为1。但是在这样做的时候,我得到了以下错误:我应该在代码中更改什么才能成功地将ArrayList转换为数组

    这是代码-->

    import java.util.ArrayList;
    class HelloWorld {
        static ArrayList<Integer> ar = new ArrayList<>();
        public static int[] printNos(int x) {
            // Write Your Code Here
            if(x>0){
                ar.add(x);
                printNos(x-1);
            }
            int [] arr = new int[ar.size()];
            arr = ar.toArray(arr);
            return arr;
    
        }
        public static void main(String[] args) {
            int [] ark = printNos(5);
            for(Interger e: ark){
                System.out.print(e);
            }
        }
    }
    

    并且正在发生以下错误-->

    错误!

    javac /tmp/kEUqQIfOZV/HelloWorld.java
    /tmp/kEUqQIfOZV/HelloWorld.java:11: error: no suitable method found for toArray(int[])
            arr = ar.toArray(arr);
    ^
        method Collection.<T#1>toArray(IntFunction<T#1[]>) is not applicable
          (cannot infer type-variable(s) T#1
            (argument mismatch; int[] cannot be converted to IntFunction<T#1[]>))
        method ArrayList.<T#2>toArray(T#2[]) is not applicable
          (inference variable T#2 has incompatible bounds
            equality constraints: int
            lower bounds: Object)
      where T#1,T#2 are type-variables:
        T#1 extends Object declared in method <T#1>toArray(IntFunction<T#1[]>)
        T#2 extends Object declared in method <T#2>toArray(T#2[])
    /tmp/kEUqQIfOZV/HelloWorld.java:18: error: cannot find symbol
            for(Interger e: ark){
                ^
    symbol:   class Interger
      location: class HelloWorld
    Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
    2 errors
    

    在这里,我知道第二个错误是由第一个错误引起的,我也可以使用各种其他方法来解决这个问题,但我想了解的是,为什么这段代码中的ArrayList没有在数组中转换并抛出错误,以及我必须做些什么来避免这个错误?

    4 回复  |  直到 2 年前
        1
  •  1
  •   marcinj    2 年前

    问题是:

        int [] arr = new int[ar.size()];
        arr = ar.toArray(arr);
                 ^^^^^^^
    

    toArray is defined as :

    <T>T[]toArray(T[]a) 返回一个数组,该数组按正确的顺序(从第一个元素到最后一个元素)包含此列表中的所有元素;返回的数组的运行时类型是指定数组的类型。

    所以它使用java泛型,泛型处理对象类型而不是基元,所以T不能是int。

    要修复它,您可以更改 int [] Integer [] 在您的代码中。或者使用一些惯用的转换代码,如下所示: How to convert an ArrayList containing Integers to primitive int array?

    …第二个错误是打字错误:

    for(Interger e: ark){
            ^ ~~~~ !
    
        2
  •  1
  •   kayasthasky    2 年前

    ArrayList是Java中Collection框架的一部分。arrayList.toArray()方法返回Object类型的数组,该数组可以是Java中任何类型的Wrapper类。

    答:您正试图将toArray()方法的输出分配给导致错误的基元类型(int[])。使用包装类Integer可以修复第一个错误。第二个错误只是打字错误。

        3
  •  0
  •   Ayush Kacholiya    2 年前

    以下代码适用于我的上述代码

    int [] arr = ar.stream().mapToInt(i -> i).toArray();
    

    用于将ArrayList转换为数组

        4
  •  0
  •   Partha Sarathi    2 年前
    1. 第一个错误 :您已写 toArray 方法错误。 T[] toArray(T[] a) 只允许泛型类型数组作为参数,并且从现在起 泛型类型不适用于基元数据类型。

    2. 第二个错误 :这只是一个 打字错误问题 ,由于之前的错误而未发生。它是 整数 整型 .

    3. 建议 :为什么要返回基元数组?当您在列表中已经有了所需的输出时!你可以简单地打印这些数据,为什么要使事情复杂化?

    import java.util.ArrayList;
    class HelloWorld {
        static ArrayList<Integer> ar;
        public static void printNos(int x) {
            // Write Your Code Here
            if(x>0){
                ar.add(x);
                printNos(x-1);
            }
    
        }
        public static void main(String[] args) {
            ar = new ArrayList<>();
            printNos(5);
            for(Integer e: ar){
                System.out.print(e + " ");
            }
        }
    }
    
    1. 根据注释更新 :如果你学习列表到数组的转换,只需使用Integer[],或者你也可以使用stream或simple-for-loop。也不要遵循这种格式的代码。在这里 您不必要地在每个递归函数中创建新的大小为n的int[]对象。 只需在主函数中移动列表到数组的转换代码。这只会创建一次对象。(更好的内存管理)