Java 如何在数组中插入一系列元素?

Java 如何在数组中插入一系列元素?,java,arrays,Java,Arrays,如何在数组中插入一系列元素 在数组中插入一个元素的当前代码如下: public static void getArrayElement() { try { if(initialSize==1) { //Get the user input System.out.print("Enter the element: "); getElement = key.nextInt();

如何在数组中插入一系列元素

在数组中插入一个元素的当前代码如下:

public static void getArrayElement()
{

    try
    {
        if(initialSize==1)
        {
            //Get the user input
            System.out.print("Enter the element: ");
            getElement = key.nextInt();

            //Assign the user input to the array
            for(int i=0; i<index; i++)
            {
                array[i] = getElement;
            }

        }

        //If the size of the array is not 1 use this
        else
        {
            //Gets the user input
            System.out.print("Enter the element: ");
            getElement = key.nextInt();

            //Create a new empty array with a new size
            int[] temp = new int[index];

            //Assign the old array into the new array
            for(int j = 0; j < index-1; j++)
            {
                temp[j] = array[j];
            }

            //Change the size of the old array
            array = new int [index];

            //Assign the temporary array into the new array with its new size
            for(int aSize = 0; aSize< array.length; aSize++)
            {
                array[aSize] = temp[aSize];
                int k = array.length;
                array[k-1] = getElement;
            }

            //Pass the array into sortArray method for sorting
            sortArray(array, index);

         }

            //Increment the index and initialSize
           index++;
           initialSize++;
      }
      catch(InputMismatchException e)
     {
            System.out.println("Invalid Input");
            System.exit(0);
     }

  }
publicstaticvoid getArrayElement()
{
尝试
{
if(initialSize==1)
{
//获取用户输入
System.out.print(“输入元素:”);
getElement=key.nextInt();
//将用户输入分配给数组

对于(int i=0;i如果
是一个
扫描仪
,您可以这样做:

while( key.hasNextInt() ) {
  int i = key.nextInt();
  //do with i whatever you want
}
我还建议使用列表而不是数组。然后将列表传递给
Collections.sort(list)
以保持其排序

虽然列表仅允许添加对象,但您可以使用自动装箱,并在插入时将
int
转换为
Integer

另外,还有一些库支持原始列表——例如,Google Guava和Apache Commons应该提供一些


另一种选择可能是排序集合,如Apache Common的
TreeBag
,它允许您将多个重复项添加到排序结构中。

如果
key
扫描仪,您可以执行以下操作:

while( key.hasNextInt() ) {
  int i = key.nextInt();
  //do with i whatever you want
}
我还建议使用列表而不是数组。然后将列表传递给
Collections.sort(list)
以保持其排序

虽然列表仅允许添加对象,但您可以使用自动装箱,并在插入时将
int
转换为
Integer

另外,还有一些库支持原始列表——例如,Google Guava和Apache Commons应该提供一些


另一种选择可能是排序集合,如Apache Common的
TreeBag
,它允许您将多个重复项添加到排序结构中。

在找到答案之前,最好先了解数组是什么

数组是存储在连续内存中的一组同构元素 地点

这就限制了数组在初始化时需要知道大小,以便从内存中保留足够的空间。这使得在父数组中根本不可能插入。为此,您必须执行arraycopy或创建具有不同大小的新数组

比如说,

int[] array = new int[10]; //Initializes an integer array of size 10.
for(int i=0;i<10;i++){
     array[i] = i;
}//stores values from 0 to 9.
int[]array=new int[10];//初始化大小为10的整数数组。

对于(inti=0;i,在找到答案之前,最好先了解数组是什么

数组是存储在连续内存中的一组同构元素 地点

这就限制了数组在初始化时需要知道大小,以便从内存中保留足够的空间。这使得在父数组中根本不可能插入。为此,您必须执行arraycopy或创建具有不同大小的新数组

比如说,

int[] array = new int[10]; //Initializes an integer array of size 10.
for(int i=0;i<10;i++){
     array[i] = i;
}//stores values from 0 to 9.
int[]array=new int[10];//初始化大小为10的整数数组。

对于(inti=0;i您希望将temp的内容复制到数组中。您最多可以通过两个步骤实现这一点

if(临时长度>数组长度)
数组=新整数[温度长度];

System.arraycopy(temp,0,array,0,temp.length);

您希望将temp的内容复制到数组中。您最多可以通过两个步骤实现这一点

if(临时长度>数组长度)
数组=新整数[温度长度];

System.arraycopy(temp,0,array,0,temp.length);

只是为了记录:
void getArrayElement()
是非常误导人的,因为
getXXX()
表示返回值。其他问题:是否
扫描器
?为什么不使用列表而不是数组?这样,您就不必自己处理复制。如果需要对集合进行排序,为什么不使用PriorityQueue?如果不必,则使用ArrayList?那么如何修改到允许输入随机数这是您的代码还是您正试图修改其他人的代码?请注意:
void getArrayElement()
非常容易引起误解,因为
getXXX()
表示返回值。其他问题:是否
扫描器
?为什么不使用列表而不是数组?这样,您就不必自己处理复制。如果需要对集合进行排序,为什么不使用PriorityQueue?如果不必,则使用ArrayList?那么如何修改到允许输入随机数这是您的代码,还是您正试图修改其他人的代码?有关方法的更多信息,请参阅