使 arrayList.toArray() 返回更具体的类型

发布于 2024-10-18 05:45:40 字数 212 浏览 3 评论 0原文

因此,通常 ArrayList.toArray() 会返回 Object[] 类型......但假设它是一个 对象 CustomArraylist,如何使 toArray() 返回 Custom[] 类型而不是对象[]

So, normally ArrayList.toArray() would return a type of Object[]....but supposed it's an
Arraylist of object Custom, how do I make toArray() to return a type of Custom[] rather than Object[]?

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

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

发布评论

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

评论(7

离鸿 2024-10-25 05:45:40

像这样:

List<String> list = new ArrayList<String>();

String[] a = list.toArray(new String[0]);

在 Java6 之前,建议这样写:

String[] a = list.toArray(new String[list.size()]);

因为内部实现无论如何都会重新分配一个适当大小的数组,所以你最好提前完成它。由于 Java6 首选空数组,请参阅 .toArray(new MyClass[0]) 或 .toArray(new MyClass[myList.size()]) ?

如果您的列表类型不正确,您需要在调用 toArray 之前进行强制转换。像这样:

    List l = new ArrayList<String>();

    String[] a = ((List<String>)l).toArray(new String[l.size()]);

Like this:

List<String> list = new ArrayList<String>();

String[] a = list.toArray(new String[0]);

Before Java6 it was recommended to write:

String[] a = list.toArray(new String[list.size()]);

because the internal implementation would realloc a properly sized array anyway so you were better doing it upfront. Since Java6 the empty array is preferred, see .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?

If your list is not properly typed you need to do a cast before calling toArray. Like this:

    List l = new ArrayList<String>();

    String[] a = ((List<String>)l).toArray(new String[l.size()]);
卸妝后依然美 2024-10-25 05:45:40

将 List 转换为特定类型(例如 Long)的 Array 的较短版本:(

Long[] myArray = myList.toArray(Long[]::new);

可从 java 11 获得)

A shorter version of converting List to Array of specific type (for example Long):

Long[] myArray = myList.toArray(Long[]::new);

(available from java 11)

遇到 2024-10-25 05:45:40

它实际上并不需要返回 Object[],例如:-

    List<Custom> list = new ArrayList<Custom>();
    list.add(new Custom(1));
    list.add(new Custom(2));

    Custom[] customs = new Custom[list.size()];
    list.toArray(customs);

    for (Custom custom : customs) {
        System.out.println(custom);
    }

这是我的 Custom 类:-

public class Custom {
    private int i;

    public Custom(int i) {
        this.i = i;
    }

    @Override
    public String toString() {
        return String.valueOf(i);
    }
}

It doesn't really need to return Object[], for example:-

    List<Custom> list = new ArrayList<Custom>();
    list.add(new Custom(1));
    list.add(new Custom(2));

    Custom[] customs = new Custom[list.size()];
    list.toArray(customs);

    for (Custom custom : customs) {
        System.out.println(custom);
    }

Here's my Custom class:-

public class Custom {
    private int i;

    public Custom(int i) {
        this.i = i;
    }

    @Override
    public String toString() {
        return String.valueOf(i);
    }
}
缱绻入梦 2024-10-25 05:45:40
 public static <E> E[] arrayListToTypedArray(List<E> list) {

    if (list == null) {
      return null;
    }
    int noItems = list.size();
    if (noItems == 0) {
      return null;
    }

    E[] listAsTypedArray;
    E typeHelper = list.get(0);

    try {
      Object o = Array.newInstance(typeHelper.getClass(), noItems);
      listAsTypedArray = (E[]) o;
      for (int i = 0; i < noItems; i++) {
        Array.set(listAsTypedArray, i, list.get(i));
      }
    } catch (Exception e) {
      return null;
    }

    return listAsTypedArray;
  }
 public static <E> E[] arrayListToTypedArray(List<E> list) {

    if (list == null) {
      return null;
    }
    int noItems = list.size();
    if (noItems == 0) {
      return null;
    }

    E[] listAsTypedArray;
    E typeHelper = list.get(0);

    try {
      Object o = Array.newInstance(typeHelper.getClass(), noItems);
      listAsTypedArray = (E[]) o;
      for (int i = 0; i < noItems; i++) {
        Array.set(listAsTypedArray, i, list.get(i));
      }
    } catch (Exception e) {
      return null;
    }

    return listAsTypedArray;
  }
梦中的蝴蝶 2024-10-25 05:45:40

我得到了答案...这似乎工作得很好

public int[] test ( int[]b )
{
    ArrayList<Integer> l = new ArrayList<Integer>();
    Object[] returnArrayObject = l.toArray();
    int returnArray[] = new int[returnArrayObject.length];
    for (int i = 0; i < returnArrayObject.length; i++){
         returnArray[i] = (Integer)  returnArrayObject[i];
    }

    return returnArray;
}

I got the answer...this seems to be working perfectly fine

public int[] test ( int[]b )
{
    ArrayList<Integer> l = new ArrayList<Integer>();
    Object[] returnArrayObject = l.toArray();
    int returnArray[] = new int[returnArrayObject.length];
    for (int i = 0; i < returnArrayObject.length; i++){
         returnArray[i] = (Integer)  returnArrayObject[i];
    }

    return returnArray;
}
白日梦 2024-10-25 05:45:40

以下解决方案适用于我的情况。

import java.util.List;
import java.util.ArrayList;

public class HelloWorld{

     public static void main(String []args){
        List<int[]> result = new ArrayList<>();
        result.add(new int[]{0, 0});
        result.add(new int[]{1, 1});
        result.add(new int[]{2, 2});
        
        int[][] temp = result.toArray(int[][]::new);
        for(int[] value: temp) {
            System.out.println("{" + value[0] + ", " + value[1] + "}");
        }
     }
}

The following solution is for my case.

import java.util.List;
import java.util.ArrayList;

public class HelloWorld{

     public static void main(String []args){
        List<int[]> result = new ArrayList<>();
        result.add(new int[]{0, 0});
        result.add(new int[]{1, 1});
        result.add(new int[]{2, 2});
        
        int[][] temp = result.toArray(int[][]::new);
        for(int[] value: temp) {
            System.out.println("{" + value[0] + ", " + value[1] + "}");
        }
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文