How to increase array size in java?

I want to store as many elements as the user needs in an array. But how to do that? If I were to create an array, I had to do this with a fixed size. Every time a new element is added to the array and the array is filled, I want to update its size to "1". I am tired of different types of code ... but it did not work. It would be very helpful if yall could give me some solutions regarding this .. in code, if possible. Thanks.

+4
source share
8 answers

Instead of using an array, use the java.util.List implementation, such as ArrayList . ArrayList has an array that contains the values ​​in the list, but the size of the array is automatically processed by the list.

 ArrayList<String> list = new ArrayList<String>(); list.add("some string"); 

You can also convert a list to an array using list.toArray(new String[list.size()]) , etc. for other types of elements.

+6
source

You can create a temporary array with a size that is one element larger than the original, and then copy the original elements to temp and assign the temporary array to a new one.

 public void increaseSize() { String[temp] = new String[original.length + 1]; for (int i = 0; i < original.length; i++){ temp[i] = original[i]; } original = temp; } 

You can resize in different ways, but the same idea applies.

+3
source

When using the copyOf method in java.util.Arrays class String[] size increases automatically / dynamically.

 package com.google.service; import java.util.Arrays; public class StringArrayAutoIncrement { public static void main(String[] args) { String[] data = new String[] { "a", "b", "c", "d", "e" }; String[] array = new String[0];// array initialize with zero int incrementLength = 1; for (int i = 0; i < data.length; i++) { array = Arrays.copyOf(data, i + incrementLength);// increment by +1 } /** * values of array after increment */ for (String value : array) { System.out.println(value); } } } 

Output:

 a b c d e 
+2
source

You can not. You can create a new array and transfer elements to this array. Or you can use an ArrayList.

+1
source

Create a list object, add items, and convert this list object to an array using list.toArray (new String [list.size ()])

+1
source

u can use a list of arrays for this

Here is an example for a list of row strings

'import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList;

open class Ex01 {

 public static void main(String[] args) throws IOException { BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in)); ArrayList<String> myArr = new ArrayList<String>(); myArr.add("Italian Riviera"); myArr.add("Jersey Shore"); myArr.add("Puerto Rico"); myArr.add("Los Cabos Corridor"); myArr.add("Lubmin"); myArr.add("Coney Island"); myArr.add("Karlovy Vary"); myArr.add("Bourbon-l'Archambault"); myArr.add("Walt Disney World Resort"); myArr.add("Barbados"); System.out.println("Stupid Vacation Resort Adviser"); System.out.println("Enter your name:"); String name = userInput.readLine(); Integer nameLength = name.length(); if (nameLength == 0) { System.out.println("empty name entered"); return; } Integer vacationIndex = nameLength % myArr.size(); System.out.println("\nYour name is "+name+", its length is " + nameLength + " characters,\n" + "that why we suggest you to go to " + myArr.get(vacationIndex)); } 

} '

Similarly, u can create an array for integer, blooean, and all data types

for more details you can see this

http://www.anyexample.com/programming/java/java_arraylist_example.xml

0
source

At a low level, you can do this as follows:

 long[] source = new long[1]; long[] copy = new long[source.length + 1]; System.arraycopy(source, 0, copy, 0, source.length); source = copy; 

Arrays.copyOf () does the same.

0
source

Use ArrayList. The size automatically increases if you try to add an ArrayList to the full array.

-2
source

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


All Articles