How to increase the size of an array in java?

2019-02-17 02:27发布

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

标签: java arrays size
9条回答
狗以群分
2楼-- · 2019-02-17 03:02

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

查看更多
beautiful°
3楼-- · 2019-02-17 03:03

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
查看更多
干净又极端
4楼-- · 2019-02-17 03:05

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

查看更多
爷的心禁止访问
5楼-- · 2019-02-17 03:07

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.

查看更多
Root(大扎)
6楼-- · 2019-02-17 03:10
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);
}
}
查看更多
贼婆χ
7楼-- · 2019-02-17 03:12

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.

查看更多
登录 后发表回答