How to increase the size of an array in java?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 03:35:07

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.

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.

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

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.

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

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

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

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);
}
}

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!