Java dynamic array sizes?

2018-12-31 07:49发布

I have a class - xClass, that I want to load into an array of xClass so I the declaration:

xClass mysclass[] = new xClass[10];
myclass[0] = new xClass();
myclass[9] = new xClass();

However, I don't know if I will need 10. I may need 8 or 12 or any other number for that matter. I won't know until runtime. Can I change the number of elements in an array on the fly? If so, how?

标签: java
18条回答
柔情千种
2楼-- · 2018-12-31 08:25

You can do some thing

private  static Person []  addPersons(Person[] persons, Person personToAdd) {
    int currentLenght = persons.length;

    Person [] personsArrayNew = Arrays.copyOf(persons, currentLenght +1);
    personsArrayNew[currentLenght]  = personToAdd;

    return personsArrayNew;

}
查看更多
高级女魔头
3楼-- · 2018-12-31 08:27

As other users say, you probably need an implementation of java.util.List.

If, for some reason, you finally need an array, you can do two things:

  • Use a List and then convert it to an array with myList.toArray()

  • Use an array of certain size. If you need more or less size, you can modify it with java.util.Arrays methods.

Best solution will depend on your problem ;)

查看更多
只若初见
4楼-- · 2018-12-31 08:27

Where you declare the myclass[] array as :

xClass myclass[] = new xClass[10]

, simply pass in as an argument the number of XClass elements you'll need. At that point do you know how many you will need? By declaring the array as having 10 elements, you are not declaring 10 XClass objects, you're simply creating an array with 10 elements of type xClass.

查看更多
旧人旧事旧时光
5楼-- · 2018-12-31 08:27

It is a good practice get the amount you need to store first then initialize the array.

for example, you would ask the user how many data he need to store and then initialize it, or query the component or argument of how many you need to store. if you want a dynamic array you could use ArrayList() and use al.add(); function to keep adding, then you can transfer it to a fixed array.

//Initialize ArrayList and cast string so ArrayList accepts strings (or anything
ArrayList<string> al = new ArrayList(); 
//add a certain amount of data
for(int i=0;i<x;i++)
{
  al.add("data "+i); 
}

//get size of data inside
int size = al.size(); 
//initialize String array with the size you have
String strArray[] = new String[size]; 
//insert data from ArrayList to String array
for(int i=0;i<size;i++)
{
  strArray[i] = al.get(i);
}

doing so is redundant but just to show you the idea, ArrayList can hold objects unlike other primitive data types and are very easy to manipulate, removing anything from the middle is easy as well, completely dynamic.same with List and Stack

查看更多
有味是清欢
6楼-- · 2018-12-31 08:27

Java Array sizes are fixed , You cannot make dynamic Arrays as that of in C++.

查看更多
梦寄多情
7楼-- · 2018-12-31 08:28

Here's a method that doesn't use ArrayList. The user specifies the size and you can add a do-while loop for recursion.

import java.util.Scanner;
    public class Dynamic {
        public static Scanner value;
        public static void main(String[]args){
            value=new Scanner(System.in);
            System.out.println("Enter the number of tests to calculate average\n");
            int limit=value.nextInt();
            int index=0;
            int [] marks=new int[limit];
            float sum,ave;
            sum=0;      
            while(index<limit)
            {
                int test=index+1;
                System.out.println("Enter the marks on test " +test);
                marks[index]=value.nextInt();
                sum+=marks[index];
                index++;
            }
            ave=sum/limit;
            System.out.println("The average is: " + ave);
        }
    }
查看更多
登录 后发表回答