如何将数组中的所有值向下移动一个位置并将最后一个值放入Java中的数组中?

时间:2014-02-14 17:31:19

标签: java

大家都是Java专家, 我需要一个引导,它将数组中的所有值向下移动一个位置,并将最后一个值放入数组中,以便将新输入添加到Java中数组的开头。程序保持循环,元素不断向右移动。以下是我的代码,请帮忙。

import java.util.Scanner;

public class ass3a 
{ 
    public static void main(String []args) 
    { 
        Scanner reader = new Scanner(System.in); 
        double calculate [] = new double [10]; 
        double read; 
        String choice; 
        while (true) 
        { 
            System.out.println("\n the first index is "+calculate[0]); 
            System.out.println("1)Add \n2)Drop \n3)Stop:"); 
            choice=reader.nextLine(); 
            char choose =choice.charAt(0); 

            switch (choose) 
            { 
                case '1': 
                Add( calculate); 
                break; 
                default: System.out.println("No such an option."); 

            } 

        } 
    } 


    public static double Add (double calculate[]) 
    { 

        Scanner reader = new Scanner(System.in); 
        double replace=0; 
        for(int i = 0; i < 10; i++) 
        { 
            if(calculate[i] == 0) 
            { 

                System.out.println("-----Adding-----"); 
                System.out.print("Please enter a number to add into the array:"); 
                calculate[i]=reader.nextDouble(); 
                System.out.println(calculate[i]); 
                replace = calculate[i]; 

                break; 

            } 

        } 

        return replace; 
    } 

}

1 个答案:

答案 0 :(得分:0)

我为你尝试了几行代码,它会删除数组中的最后一个元素......

 public static double drop (double calculate[]) 
{ 
    double replace=0; 
    System.out.println("-----dropping-----"); 
    System.out.print("Please enter a number to add into the array:"); 
    Scanner reader = new Scanner(System.in);

    replace=reader.nextDouble();
    double temp;


    //calculate.length calculates the length of an array
     int len=calculate.length-1;



    //removing the last element of an array and shifting down the entire array 
    for(int i = 0; i <len; i++) 
    {   
    calculate[len-i]=calculate[len-(i+1)];
    }   
    calculate[0]=replace;

    System.out.println("Array now contains...");
    for(int j=0;j<calculate.length;j++)         
        System.out.println(calculate[j]);

    return replace; 
}     

使用此方法的缺点是 - 这里数组的最后一个元素在最后输入的值上被删除。我已经完成了修改你的add方法,其中第一个元素存储在数组的最后一部分,现在代码看起来像这样..

    import java.util.Scanner;

    public class pg1 
    { 

    //Declared for easy access of arrays..
static int temp1;

    public static void main(String []args) 
    { 
    Scanner reader = new Scanner(System.in); 
    double calculate [] = new double [10];

    //Finding the length..  Have used a variable to increase the flexibility of the code. 
    //incase, if you want to change the array length at any point

    temp1=calculate.length-1;


    double read; 
    String choice; 
    while (true) 
    { 
        System.out.println("\n the first index is "+calculate[0]); 
        System.out.println("1)Add \n2)Drop \n3)Stop:"); 
        choice=reader.nextLine(); 
        char choose =choice.charAt(0); 

        switch (choose) 
        { 
            case '1': 
            Add( calculate); 
            break; 


            case '2':
            drop(calculate);
            break;

            default: System.out.println("No such an option.");
        } 

    } 
} 

//这个地方是进行重大修改的地方

public static double Add (double calculate[]) 
{ 

    Scanner reader = new Scanner(System.in); 
    double replace=0; 

    //Only enters when the first element of the code is zero... Zero is the default value..
        if(calculate[0] == 0) 
        { 
            //Storing the elements into array and displaying it..

            System.out.println("-----Adding-----"); 
            System.out.print("Please enter a number to add into the array:"); 
            calculate[temp1--]=reader.nextDouble(); 



            System.out.println("Array now contains...");
            for(int j=0;j<calculate.length;j++)
                System.out.println(calculate[j]);
        }
        return replace;
}



public static double drop (double calculate[]) 
{ 
    double replace=0; 
    System.out.println("-----dropping-----"); 
    System.out.print("Please enter a number to add into the array:"); 
    Scanner reader = new Scanner(System.in);

    replace=reader.nextDouble();
    double temp;

    int len=calculate.length-1;

    for(int i = 0; i <len; i++) 
    {   
    calculate[len-i]=calculate[len-(i+1)];
    }   
    calculate[0]=replace;

    System.out.println("Array now contains...");
    for(int j=0;j<calculate.length;j++)         
        System.out.println(calculate[j]);

    return replace; 
}     
}

快乐编码..