简体   繁体   English

Java并将List的addAll方法使用<Character>

[英]Java and using method addAll for List<Character>

I have something simple, but can't solve. 我有一些简单的方法,但无法解决。 I just want to add one list of chars to another . 我只想将一个字符列表添加到另一个。 It's working with String types. 它适用于String类型。 Why does it not work with char ? 为什么char不起作用?

Character [] myChar={'a', 's', 'd','f', 't', 'i'};
List<Character> listChar= Arrays.asList(myChar);
Character[] newChar= new Character[listChar.size()];
List<Character> newlistChar= Arrays.asList(newChar);
Collections.fill(newlistChar, 'd');
output(newlistChar,"filled d");
Collections.addAll(listChar,newlistChar); // does not work

I tried to do something like casting the list, but still got an exception. 我试图做一些像投射列表一样的事情,但是仍然有一个例外。

   ...((String)(listChar),(String)(newlistChar))

Even if I use something like this it still throws and exception. 即使我使用类似的东西,它仍然会抛出异常。 So how do I use addAll with char ? 那么,如何将addAll与char一起使用?

Collections.addAll(listChar,newlistChar.toArray(new Character[newlistChar.size()]));

Unless this is [homework] , you should use a StringBuilder or just plain String. 除非这是[homework] ,否则应使用StringBuilder或仅使用纯字符串。

// Using String
String myChars = "asdfti";
char[] ds = new char[mychars.length()];
Arrays.fill(ds, 'd');
myChars += new String(ds);
  • asList returns a fixed size list, you can't add elements to it. asList返回固定大小的列表,您不能向其中添加元素。
  • Collections.addAll doesn't accept list as 2nd parameter Collections.addAll不接受列表作为第二个参数

Use next: 使用下一个:

    Character[] myChar = {'a', 's', 'd', 'f', 't', 'i'};
    List<Character> listChar = new ArrayList<Character>(Arrays.asList(myChar));
    Character[] newChar = new Character[listChar.size()];
    Arrays.fill(newChar, 'd');
    Collections.addAll(listChar, newChar);

List returned from Arrays.asList has fixed size. Arrays.asList返回的List具有固定大小。 You cannot add anything to it. 您不能向其中添加任何内容。

Instead, add both lists to new ArrayList . 而是将两个列表都添加到新的ArrayList中

Have you read the javadoc of Collections.addAll() ? 您阅读过Collections.addAll()javadoc吗?

Its signature is 它的签名是

public static <T> boolean addAll(Collection<? super T> c,
                                 T... elements)

This means that the second argument is a varargs. 这意味着第二个参数是一个变量。 You may thus pass an array of Characters, or individual Character instances: 因此,您可以传递一个字符数组或单个字符实例:

Collections.addAll(listChar, new Character[] {'a', 'b'});
Collections.addAll(listChar, Character.valueOf('a'), Character.valueOf('b'));

But a List<Character> is an invalid type for the second argument. 但是List<Character>是第二个参数的无效类型。

A good way to know is to actually read the compiler error message. 知道的一个好方法是实际读取编译器错误消息。 Usually, they're much more informative than "it doesn't work". 通常,它们比“不起作用”提供更多信息。

And another problem is that the lists returned by Arrays.asList() have a fixed size. 另一个问题是Arrays.asList()返回的列表的大小是固定的。 You can't add anything to them. 您无法向他们添加任何内容。

Would this code snippet help? 该代码段有帮助吗?

    ArrayList<Character> myChar = new ArrayList<Character>();
    myChar.add('a');
    myChar.add('s'); 
    myChar.add('d');
    myChar.add('f'); 
    myChar.add('t'); 
    myChar.add('i');

    ArrayList<Character> newlistChar= new ArrayList<Character>();
    newlistChar.add('d');

    myChar.addAll(newlistChar);

change the last line to 将最后一行更改为

Collections.addAll(listChar, newlistChar.toArray(new Character[newlistChar.size()])); Collections.addAll(listChar,newlistChar.toArray(new Character [newlistChar.size()]));

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

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