How to increase the size of an array in java?

2019-02-17 03:20发布

问题:

I want to store as many elements as desired by the user in an array.But how do i do it??,If i were to create an array,i must do so with a fixed size. Every time a new element is added to the array and the array becomes full,i want to update its size by '1'.I tired various types of code..but.it did not work out.It would be of great help if yall could give me some solutions regarding it..in code if possible.Thank You

回答1:

Instead of using an array, use an implementation of java.util.List such as ArrayList. An ArrayList has an array backend which holds values in a list, but the array size is automatically handles by the list.

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

You can also convert the list into an array using list.toArray(new String[list.size()]) and so forth for other element types.



回答2:

You can create a temporary array with a size that is one element larger than the original, and then copy the elements of the original into the temp, and assign the temporary array to the 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 change the size in various ways, but the same idea applies.



回答3:

You can't. You can either create a new array and move the items to that array - Or you can use an ArrayList.



回答4:

By using copyOf method in java.util.Arrays class String[] size is increment 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


回答5:

On a low level you can do it this way:

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

Arrays.copyOf() is doing same thing.



回答6:

Create list object, add the elements and convert that list object to array using list.toArray(new String[list.size()])



回答7:

u can use array list for that

here is example for array list of string

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

public 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's why we suggest you to go to " 
                    + myArr.get(vacationIndex));
} 

}'

similarly u can make array for integer,blooean and all kinds of data types

for more detail u can see this

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



回答8:

public class IncreaseArraySize {
 public static void main(String[] args) {
    int arr[] = new int[5];
      for (int i = 0; i < arr.length; i++) {
            arr[i] = 5;
      }
      for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
      }
       System.out.println(" ");
       System.out.println("Before increasing Size of an array :" + arr.length);


        int arr2[] = new int[10];

        arr = arr2;
        arr2 = null;
        for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ");
    }
    System.out.println("");
    System.out.println("After increasing Size of an array : " + arr.length);
}
}


回答9:

Use an ArrayList. The size it automatically increased if you try to add to a full ArrayList.



标签: java arrays size