【问题标题】:push a new item into the end of the array [closed]将一个新项目推入数组的末尾[关闭]
【发布时间】:2016-10-14 16:02:13
【问题描述】:

所以我想将一个新项目推送到数组的末尾(数字 1-9)。一个朋友告诉我,我写的代码是正确的,但是当我在eclipse上运行它时,什么都没有发生。我需要做什么?我应该只在主块下打印数组吗?谢谢。

public static void main(String[] args) {
    long[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}

public static long[] push(long[] array, long item) {

    // Add one item to an array
    long cellsToAdd = 1;
    long[] array2 = new long[array.length + 1];

    // copy each item in array2 -> array3
    for (int i = 0; i < array.length; i++) {
        array[i] = array2[i];
    }

    array2[array2.length - 1] = item;

    System.out.println("Array: ");
    // Print the array to the console
    for (long p : array2) {
        System.out.println(p);
    }

    return array2;
}

【问题讨论】:

  • 你没有调用函数
  • 你应该从 main 方法调用push
  • 还要注意,Java 数组具有固定长度。你不能把任何东西推到最后。相反,您提供的代码会创建一个新的、更长的数组,其中包含所需的内容。这本身并没有什么问题,但重要的是要理解这与你所问的问题之间的区别。

标签: java arrays eclipse


【解决方案1】:

您可以使用System.arraycopy 创建一个新数组并增加其大小。

由于您使用的是原语long 类型,因此如果您想支持这些类型,则需要为每个原语(intfloatdouble 等)复制粘贴此逻辑。

public static void main(String[] args) {
    long[] digs = { 0, 1, 2, 3, 5 }                       
    long[] digs2 = push(digs, 6);
    long[] digs3 = pushAll(digs2, new long[] { 7, 8, 9 });

    System.out.println(Arrays.toString(digs));   // [0, 1, 2, 3, 5]
    System.out.println(Arrays.toString(digs2));  // [0, 1, 2, 3, 5, 6]
    System.out.println(Arrays.toString(digs3));  // [0, 1, 2, 3, 5, 6, 7, 8, 9]

    // Generic Example
    Long[] genArr = push(new Long[] { 0L, 1L }, new Long(3L), Long.class);

    // or Long[] genArr = push(new Long[] { 0L, 1L }, new Long(3L));

    System.out.println(Arrays.toString(genArr)); // [0, 1, 3] 
}

public static long[] push(long[] a, long b) {
    long[] result = new long[a.length + 1]; 
    System.arraycopy(a, 0, result, 0, a.length); 
    result[a.length] = b;
    return result;
} 

全部推送

public static long[] pushAll(long[] a, long[] b) {
    long[] result = new long[a.length + b.length]; 
    System.arraycopy(a, 0, result, 0, a.length); 
    System.arraycopy(b, 0, result, a.length, b.length); 
    return result;
}

通用推送

public static <E> E[] push(E[] a, E b, Class<E> classType) {
    @SuppressWarnings("unchecked")
    E[] result = (E[]) Array.newInstance(classType, a.length + 1);
    System.arraycopy(a, 0, result, 0, a.length); 
    result[a.length] = b;
    return result;
}

可选

// Convenience, so that you don't have to pass in the class.
public static Long[] push(Long[] a, Long b) {
    return push(a, b, Long.class);
}

【讨论】:

    【解决方案2】:

    除了像其他人提到的那样实际调用函数之外,您的 for 循环从 array2 复制到数组,而它应该是相反的方式

    【讨论】:

      【解决方案3】:

      调用方法

      你定义了一个方法但没有调用它。

      public static void main(String[] args) {
          long[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
          array = MyClassName.push( array , 10L );
      }
      

      如果您只是为了学习而练习,那就足够了。但是要知道,Java Collections 框架中已经为您提供了这种功能。

      【讨论】:

        猜你喜欢
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        • 2018-03-28
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多