How to add an element to the end of an array?

I want to know how to add or add a new element to the end of an array. Is there an easy way to add an element at the end? I know how to use a StringBuffer, but I don't know how to use it to add an element to an array. I prefer it without an ArrayList or list. I wonder if StringBuffer will work with integers.

+8
source share
5 answers

You cannot add an element to an array, since arrays in Java have a fixed length. However, you can create a new array from an existing one using Arrays.copyOf(array, size) :

 public static void main(String[] args) { int[] array = new int[] {1, 2, 3}; System.out.println(Arrays.toString(array)); array = Arrays.copyOf(array, array.length + 1); //create new array from old array and allocate one more element array[array.length - 1] = 4; System.out.println(Arrays.toString(array)); } 

I would still recommend giving up working with an array and using List .

+8
source

Arrays in Java have a fixed length that cannot be changed. Therefore, Java provides classes that allow you to maintain variable-length lists.

Typically, there is a List<T> interface that represents a list of instances of class T The simplest and most widely used implementation is ArrayList . Here is an example:

 List<String> words = new ArrayList<String>(); words.add("Hello"); words.add("World"); words.add("!"); 

List.add() just adds the item to the list, and you can get the size of the list using List.size() .

+3
source

The OP says for unknown reasons: "I prefer it without an arraist or a list."

If the type you are referring to is primitive (you specify integers, but you don't say if you mean int or Integer ), then you can use one of the NIO buffer classes, for example java.nio.IntBuffer . They are very similar to StringBuffer - they act as buffers for a list of primitive type (buffers exist for all primitives, but not for objects), and you can wrap the buffer around the array and / or extract the array from the buffer.

Note that javadocs say: "The buffer volume is never negative and never changes." This is still just a wrapper around the array, but it's better to work with it. The only way to effectively expand the buffer is to allocate() a larger one and use put() to flush the old buffer to the new one.

If it's not primitive, you should just use List , or come up with a compelling reason why you can't or don't want, and maybe someone will help you get around this.

+1
source

As many others pointed out, if you are trying to add a new element at the end of the list, then something like array [array.length-1] = x; should make. But this will replace the existing element.

For something like continuously adding to an array. You can keep track of the index and continue to add elements until you reach the goal and have a function that does the addition, returning you the next index, which in turn will tell you how many more elements can fit into the array.

Of course, in both cases, the size of the array will be predetermined. A vector may be your other option, since you do not want an arraylist that will allow you all the same functions and functions and will take care of increasing the size in addition.

Going to the part where you want the StringBuffer array. I believe that you are looking for the getChars method (int srcBegin, int srcEnd, char [] dst, int dstBegin). See what can solve your doubts. Again, I would like to note that after you have deleted an array from it, you can still replace only the last existing element (symbol in this case).

+1
source

To clarify the terminology, it’s true: arrays are structures of a fixed length (and the length of an existing one cannot be changed), the add statement at the end is pointless (in itself).

What you can do is create a new array one more element and fill in a new element in the last slot:

 public static int[] append(int[] array, int value) { int[] result = Arrays.copyOf(array, array.length + 1); result[result.length - 1] = value; return result; } 

This quickly becomes inefficient, because every time the append is called, a new array is created, and the contents of the old array are copied.

One way to dramatically reduce overhead is to create a larger array and keep track of which index it is actually filled to. Adding an item becomes just filling the next index and increasing the index. If the array is completely full, a new array is created with plenty of free space.

And guess what ArrayList does: that is exactly what. Therefore, when a dynamic-size array is required, an ArrayList is a good choice. Do not reinvent the wheel.

0
source

Source: https://habr.com/ru/post/982573/


All Articles