返回首页

您好
我的项目的数组在Java
我想在代码中使用随机数
怎么能行

 /*

* This is the sorted class

*/

 



 

 

import java.util.*;

 

public class SortedArray

{

    static Scanner key = new Scanner(System.in);

    static int initialSize = 1;

    static int[] array = new int[initialSize];

    static int index = 1;

    static int getElement;

    static int delElement;

 



    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);

         }

 

      }

 

    //This is a bubble sort that sorts the array

    public static void sortArray(int a[], int size)

    {

 

        for(int sortSize = 0; sortSize < size-1; sortSize++)

        {

            int temp_number;

 

            for(int secondSize = (sortSize+1); secondSize < size; secondSize++)

            {

                if(a[sortSize] > a[secondSize])

                {

                    temp_number = a[sortSize];

                    a[sortSize] = a[secondSize];

                    a[secondSize] = temp_number;

                }

            }

 

        }

 

    }

 

    //Gets the sorted array

    public static int[] getArray()

    {

        return array;

    }

 

    public static void printArray()

    {

        int getSize = 0;

        //Outputs the sorted array

        for(int get =0; get < getArray().length; get++)

        {

            int[] tempArray = getArray();

            System.out.println(tempArray[get]);

            getSize++;

 

        }

            System.out.print("Size: " + getSize + "\n");

    }

 

    //Empty the array

    public static void clear()

    {

 

        index = 1;

        initialSize = 1;

        array = new int[initialSize];

        System.out.println("The array is empty now");

    }

 

    /*

    * This is the find method

    * If the target is found then delete the target in the array

    * else let the user know that target is not found

    */

    public static void search(int dE)

    {

        int target = dE;

        Boolean found = false;

 

        for(int fValue = 0; fValue < array.length; fValue++)

        {

 

                if(array[fValue]==target)

                {

                        for(int rElement = fValue; rElement < array.length-1; rElement++ )

                        {

                                array[rElement] = array[rElement+1];

                        }

 

                        int[] temp_Dele = new int[index-1];

 

                        for(int tempD = 0; tempD < index-1; tempD++)

                        {

                                temp_Dele[tempD] = array[tempD];

                        }

 

                        index--;

                        array = new int[temp_Dele.length-1];

 

                        for(int aDele = 0; aDele < array.length; aDele++)

                        {

                                array[aDele] = temp_Dele[aDele];

                        }

 

                    found = true;

 

                }

 



        }

 



        if(!found)

        {

                System.out.println("Element Not Found");

         }

 



    }

 

    //Delete the elements inside the array

    public static void deleteArray()

    {

 

            System.out.print("Enter the element to be delete:");

            delElement = key.nextInt();

 

            search(delElement);

 

    }

 

    //Printing the the smallest element of the array

    public static void smallest()

    {

            System.out.println(array[0]);

    }

 

    //Printing the largest element of the array

    public static void largest()

    {

            int lastIndex = array.length;

 

            //Printing the last element of the array

            System.out.println(array[lastIndex-1]);

    }

 

}

 

{C}以及如何计算的运行时间?

回答

评论会员:理查德MacCutchan 时间:2012/02/07
]
评论会员:游客 时间:2012/02/07
RaviRanjankr:尝试{A}{A2}{A3的}]