Answers for "how to add a number to an array in java"

9

how to append to an array in java

import java.util.Arrays;

class ArrayAppend {
    public static void main( String args[] ) {
      int[] arr = { 10, 20, 30 };
      System.out.println(Arrays.toString(arr));

      arr = Arrays.copyOf(arr, arr.length + 1);
      arr[arr.length - 1] = 40; // Assign 40 to the last element
      System.out.println(Arrays.toString(arr));
    }
}
Posted by: Guest on February-15-2021
1

how to add a number to an array in java

List<Integer> myList = new ArrayList<Integer>();
myList.add(5);
myList.add(7);
Posted by: Guest on February-21-2020

Code answers related to "how to add a number to an array in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language