Java array append new element

Description

Java array append new element

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[] myArray = { 1, 2, 3 };
    System.out.println(Arrays.toString(myArray));

    int[] tempArray = new int[myArray.length + 1];
    for (int i = 0; i < myArray.length; i++) {
      tempArray[i] = myArray[i];//from www.  j a  va  2s  .  c om
    }
    tempArray[myArray.length] = 50;
    myArray = tempArray;

    System.out.println(Arrays.toString(myArray));
  }
}



PreviousNext

Related