Java 通过将数组转换为列表、添加元素并再次将其转换回,将元素添加到数组

Java 通过将数组转换为列表、添加元素并再次将其转换回,将元素添加到数组,java,arrays,list,Java,Arrays,List,我需要将en int元素添加到数组中 我考虑将数组转换为arrayList,添加int,然后再次将arrayList转换为数组 果然,我完全失败了 aGrades是数组,lGrades是数组列表 // add one grade from 1-5 public void enter (int grade){ ArrayList<Integer> lGrades = new ArrayList<Integer>(Arrays.asList(aGrades))

我需要将en int元素添加到数组中

我考虑将数组转换为arrayList,添加int,然后再次将arrayList转换为数组

果然,我完全失败了

aGrades是数组,lGrades是数组列表

// add one grade from 1-5
    public void enter (int grade){
    ArrayList<Integer> lGrades = new ArrayList<Integer>(Arrays.asList(aGrades));
    lGrades.add(grade);
    aGrades = listArray.toArray(lGrades);
  }
//从1-5添加一个等级
公共无效输入(整数级){
ArrayList lGrades=新的ArrayList(Arrays.asList(aGrades));
lGrades.add(年级);
aGrades=listArray.toArray(lGrades);
}
目前的错误是:

Histo.java:28: error: no suitable constructor found for ArrayList(List<int[]>)
    ArrayList<Integer> lGrades = new ArrayList<Integer>(Arrays.asList(aGrades));
                                 ^
constructor ArrayList.ArrayList(Collection<? extends Integer>) is not applicable
  (actual argument List<int[]> cannot be converted to Collection<? extends Integer> by method invocation conversion)
constructor ArrayList.ArrayList() is not applicable
  (actual and formal argument lists differ in length)
constructor ArrayList.ArrayList(int) is not applicable
  (actual argument List<int[]> cannot be converted to int by method invocation conversion)
Histo.java:30: error: incompatible types
    aGrades = lGrades.toArray(new Integer[lGrades.size()]);
                             ^
  required: int[]
  found:    Integer[]
Histo.java:28:错误:找不到适合ArrayList(列表)的构造函数
ArrayList lGrades=新的ArrayList(Arrays.asList(aGrades));
^

构造函数ArrayList.ArrayList(Collection如果您的问题是编译时错误,则位于以下行:

aGrades = listArray.toArray(lGrades);
只需替换为:

aGrades = lGrades.toArray(new Integer[lGrades.size()]);

尽管我建议首先使用
列表

如果需要向数组中添加元素,最好使用和
数组列表
,而不是来回转换。如果出于某种原因,您不想这样做,则可以更有效地延长数组像这样:

int [] newAGrades = new int[aGrades.length + 1];

System.arraycopy(aGrades, 0, newAGrades, 0, aGrades.length);

newAGrades[aGrades.length] = grade;

aGrades = newAGrades;
尽管再次强调,仅使用
ArrayList
将是一个更好的主意:

aGrades.add(grade)
试一试


正如其他人所说,最好的选择是使用带有整数对象的ArrayList。如果您想使用int原语数组,最好使用数组自行调整大小

  // add one grade from 1-5
  public void enter (int grade){
    int[] aGradesTmp = new int[aGrades.length+1];
    System.arraycopy(aGrades, 0, aGradesTmp, 0, aGrades.length);
    aGradesTmp[aGrades.length] = grade;
    aGrades = aGradesTmp;
  }
上面所做的是内存和处理器效率低下。这种解决方法内存效率低下,但在处理器上效率更高,因为System.arraycopy()是作为本机方法实现的


最终,你只想尽可能远离数组,只想使用集合类。

如果你需要更改数组的大小,最好还是坚持使用ArrayList。什么失败了?有什么错误吗?如果有,发布错误。如果没有,发生了什么,以及预期会发生什么?失败是因为他在中使用了原语数组。toArray()本质上,你有两种选择,答案包括每个样本代码。如果添加一个元素是频繁的,切换到ARARYLIST。如果很少见,考虑创建一个新数组一个元素,在旧数据中复制,并在末尾赋值新元素。这实际上是令人惊讶的简单。
  // add one grade from 1-5
  public void enter (int grade){
    int[] aGradesTmp = new int[aGrades.length+1];
    System.arraycopy(aGrades, 0, aGradesTmp, 0, aGrades.length);
    aGradesTmp[aGrades.length] = grade;
    aGrades = aGradesTmp;
  }