Cannot invoke copyOf(char[], int) on the array type char[] in the function “public ArrayList<char[]> sort(ArrayList<char[]> chars)”

问题: I'm coding a function that takes in an ArrayList, then copies each of the char[] into another char[] with added length, then the new char[] is added to a new ArrayList. The...

问题:

I'm coding a function that takes in an ArrayList, then copies each of the char[] into another char[] with added length, then the new char[] is added to a new ArrayList. The error occurs in the line when I try to copy the array. Am I incorrectly using the syntax or is my logic wrong?

public ArrayList<char[]> sort(ArrayList<char[]> chars)
{
  ArrayList<char[]> chars2 = new ArrayList<char[]>();
  for (int i = 0; i < chars.size(); i++)
  {
    if (chars.get(i).length < chars.get(i + 1).length)
    {
      char[] c = chars.get(i).copyOf(chars.get(i), ((chars.get(i + 1).length - chars.get(i).length) + chars.get(i).length));
    }
  }
  return chars2;
}

回答1:

The object char[] does not have a .copyOf method. You can replace that with Arrays.copyOf(array, size)

int size = ((chars.get(i + 1).length - chars.get(i).length) + chars.get(i).length);
char[] c = Arrays.copyOf(chars.get(i), size);
  • 发表于 2019-01-06 04:34
  • 阅读 ( 243 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除