Java程序在数组中插入元素

时间:2018-11-27 02:50:14

标签: java arrays

private static int i;

    public static void main(String[] args) 
    {
        int n,num1,num3;
        int arr[] = new int[50];

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter number you want in array:");
        n = scan.nextInt();
        int a[] = new int[n];
        System.out.println("Enter any number:");
        for (int i = 0; i < n; i++) 
        {
            a[i] = scan.nextInt();
        }
        for (int i = 0; i < n; i++) 
        {
            for (int num = i + 1; num < n; num++) 
            {
                if (a[i] > a[num]) 
                {
                    num1 = a[i];
                    a[i] = a[num];
                    a[num] = num1;
                }
            }
        }

        System.out.print("Ascending Order:");
        for (int i = 0; i < n - 1; i++) 
        {
            System.out.print(a[i] + ",");
        }
        System.out.println(a[n-1]);

        System.out.println("Enter Last number:" );
        num1= scan.nextInt();

        System.out.print("New Array Position: ");
        for(i=0; i<n+1; i++)
        {
            System.out.print(arr[i] + " ");

        }
    }
}

输出:

Enter number you want in array:5
Enter any number: 5 2 3 1
Ascending Order: 1 2 3 5
Enter last number: 4
New Array Position: 0 0 0 0 

应该是这样1 2 3 4 5伙计们可以帮我谢谢

2 个答案:

答案 0 :(得分:0)

事物的结合:

1。在最后一个for循环中打印arr [i]时,声明后您从未接触过数组arr 即int arr[] = new int[50];因此,输出0 0 0 0符合预期。

2。在读取了最后一个数字(即num1= scan.nextInt();)之后,您尚未在前进的任何地方使用它。您如何看待数组才能了解要添加的最后一个元素? :)

这种事情会起作用:

1。对数组a排序后,将其复制到数组arr中:

for(int i=0;i<n;i++){
    arr[i] = a[i];
}

2。一旦将所有旧元素放在数组arr中,请读取数组arr中的最后一个元素,即arr[n+1] = scan.nextInt();

3。在这里,您可以再次对数组arr进行排序,以将最后一个元素放到适当的位置。

4。打印数组arr:

for(int j=0;j<n+1;j++){
System.out.print(arr[j]+" ");
}

注意:确保您的n小于50。否则,您将碰到java.lang.ArrayIndexOutOfBoundsException

答案 1 :(得分:0)

您的代码中有几个问题,

此答案基于给定的输出

1)您将要指定数组的大小:

根据输出为:5    但实际上您将从命令行获取4个数字,因此第一个for循环应仅针对4个元素(假设您将再次添加最后一个元素,因此所有元素都在array = 5中计数)

for (int i = 0; i < n-1; i++) 
{
   a[i] = scan.nextInt();
}

2)第二个for循环应该持续到4个元素

for (int i = 0; i < n-1; i++) 
  {
    for (int num = i + 1; num < n-1; num++) 
    {
        if (a[i] > a[num]) 
        {
            num1 = a[i];
            a[i] = a[num];
            a[num] = num1;
        }
    }
  }

3)数组的升序仅包含4个元素,因此它也一直持续到4个元素

for (int i = 0; i < n-1 ; i++) 
{
     System.out.print(a[i] + ",");
}

4)您将从命令行获取最后一个元素,它应该分配给数组

a[n-1] = num1= scan.nextInt();

5)您必须再次重新排列阵列

for (int i = 0; i < n; i++) 
{
    for (int num = i + 1; num < n; num++) 
    {
        if (a[i] > a[num]) 
        {
            num1 = a[i];
            a[i] = a[num];
            a[num] = num1;
        }
    }
}

6)然后,您必须打印数组:

for(i=0; i<n; i++)
{
    System.out.print(a[i] + " ");

}

完整代码:

public class MyClass {
    private static int i;
    public static void main(String[] args) 
    {
        int n,num1,num3;
        int arr[] = new int[50];

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number you want in array:");
        n = scan.nextInt();
        int a[] = new int[n];

        System.out.println("Enter any number:");
        for (int i = 0; i < n-1; i++) 
        {
            a[i] = scan.nextInt();
        }

        for (int i = 0; i < n-1; i++) 
        {
            for (int num = i + 1; num < n-1; num++) 
            {

                if (a[i] > a[num]) 
                {
                    num1 = a[i];
                    a[i] = a[num];
                    a[num] = num1;
                }
            }
        }

        System.out.print("Ascending Order:");
        for (int i = 0; i < n-1 ; i++) 
        {
            System.out.print(a[i] + ",");
        }
         System.out.println("");
        System.out.println("Enter Last number:" );
        a[n-1] = num1= scan.nextInt();

        for (int i = 0; i < n; i++) 
        {
            for (int num = i + 1; num < n; num++) 
            {

                if (a[i] > a[num]) 
                {
                    num1 = a[i];
                    a[i] = a[num];
                    a[num] = num1;
                }
            }
        }

        System.out.print("New Array Position: ");
        for(i=0; i<n; i++)
        {
            System.out.print(a[i] + " ");

        }
    }
}

输出:

Enter number you want in array:5

Enter any number:
5
3
2
1

Ascending Order:1,2,3,5,

Enter Last number:
4

New Array Position: 1 2 3 4 5