How to increase the size of an array in Java?

前端 未结 10 621
粉色の甜心
粉色の甜心 2021-01-04 04:00

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

相关标签:
10条回答
  • 2021-01-04 04:38

    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

    0 讨论(0)
  • 2021-01-04 04:39

    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.

    0 讨论(0)
  • 2021-01-04 04:41

    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.

    0 讨论(0)
  • 2021-01-04 04:41

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

    0 讨论(0)
提交回复
热议问题