Home >Java >javaTutorial >Learn how to insert new elements into Java array
How to insert new elements into Java array?
Array is a very commonly used data structure that can continuously store the same type of data in memory. In Java, the length of an array is fixed and cannot be changed once created. However, in some scenarios, we may need to insert new elements into the array. So, how to insert new elements into an array in Java? Detailed answers will be given below, along with corresponding code examples.
A simple method is to create a new array, put the elements to be inserted into the new array, and replace the original array with Copy the elements in to the new array. Here is the sample code for this method:
public static int[] insertElement(int[] originalArray, int element, int index) { int[] newArray = new int[originalArray.length + 1]; // 将原数组中的元素复制到新数组中 for (int i = 0, j = 0; i < originalArray.length; i++, j++) { if (i == index) { newArray[j] = element; j++; } newArray[j] = originalArray[i]; } return newArray; }
Using this method, we can call the insertElement
method to insert a new element in the array. For example, if we have an array int[] arr = {1, 2, 3, 4, 5}
and now need to insert element 6 at position 2, we can call this method like this:
int[] newArr = insertElement(arr, 6, 2);
Then, newArr
will be {1, 2, 6, 3, 4, 5}
.
The disadvantage of this method is that it requires creating a new array and copying the elements in the original array to the new array. For large arrays, this can cause performance issues.
Another method is to use Java's ArrayList class to manage arrays. ArrayList is a dynamic array that automatically resizes as needed. We can use the add
method of ArrayList to insert a new element at a specified position. Here is the sample code for this method:
import java.util.ArrayList; public static void insertElement(ArrayList<Integer> list, int element, int index) { list.add(index, element); }
Using this method, we can call the insertElement
method to insert a new element in the ArrayList. For example, if we have an ArrayListArrayList<integer> list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5))</integer>
, now we need to insert element 6 at position 2, We can call this method like this:
insertElement(list, 6, 2);
Then, the ArrayList will be [1, 2, 6, 3, 4, 5]
.
The advantage of this method is that it does not require the creation of a new array, and the ArrayList will automatically resize. However, because ArrayList is an object, it requires more memory to store additional information.
Summary
The above are two common methods of inserting new elements into Java arrays. If you need to insert new elements into an already fixed-length array, you can use the first method, which is to create a new array and copy the elements in the original array to the new array. If you need an array that can be dynamically resized, you can use the ArrayList class instead of an array. Which method you choose depends on your specific needs and performance requirements. Hope this article is helpful to you!
The above is the detailed content of Learn how to insert new elements into Java array. For more information, please follow other related articles on the PHP Chinese website!