在数组中搜索元素

时间:2021-04-12 07:34:45

标签: java indexing

我正在尝试创建一个程序,该程序询问用户数组中的元素数量并打印其原始列表反向列表并发送。此外,我还试图在数组中找到一个元素,并打印找到的元素和索引号(如果它在数组中)。到目前为止,这是我的代码:

    public static void main(String[] args){
        int n, y, z, temp=0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of elements you want to store here: ");
        n = sc.nextInt();
        int[] a = new int[10];
        System.out.println("Enter elements here: ");
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
    
        System.out.print("\nOriginal list: ");
        for(int i=0;i<n;i++){
            System.out.print(a[i]+" ");
        }
    
        System.out.print("\nReverse list: ");
        for(int i=n-1;i>=0;i--){
            System.out.print(a[i]+" ");
        }
        // it seems to copy the assending list. It should print the integers the way user entered it
        System.out.print("\nOriginal list: ");
        for(int i=0;i<n;i++){
            System.out.print(a[i]+" ");
        }
    
        for(y=0;y<n;y++){
            for(z=y+1;z<n;z++){
                if(a[y]>a[z]){
                    temp=a[y];
                    a[y]=a[z];
                    a[z]=temp;
                }
            }
        }
    
        {System.out.print("\nAscending Order list: ");
        for(y=0;y<n;y++){
            System.out.print(""+a[y]+" ");
        }}
        System.out.print("\nEnter the element you want to search: ");
        s = sc.nextInt();
        for(y=0;y<n;y++)
        {
        if(a[y]==s)
        {
        System.out.println("Element "+s+" is in "+y+" index");
        f=1;
        }
        }
        if(f==0)
        {
        System.out.println("Element "+s+" is not found");
        }
    } 
}

我的问题是最后一个原始列表。

1 个答案:

答案 0 :(得分:0)

首先,您使用的是数组而不是列表。在这里,您没有采用用户定义的数组大小。相反,您正在使用 10 个元素的数组。然后你交换了原始数组的元素。这导致数组元素被重新排列。因此,当您交换元素时,原始数组发生了变化。在这种情况下,您应该获取数组的副本。查找索引号非常容易。只需循环遍历元素就可以了。希望这会有所帮助:

public static void main(String[] args)
    {
        int n;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of elements you want to store here: ");
        n = sc.nextInt();

        int[] a = new int[n]; //Number of array member should be declared here

        System.out.println("Enter array elements here: ");

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

        System.out.print("\nOriginal array: ");


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

        System.out.print("\nReverse array: ");

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


        System.out.print("\nOriginal array: ");

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


        //This is where you should make a copy of the original array. You are swapping variable. That's mean array is also getting swapped
        System.out.print("\nAscending Order array: ");

        int[] tempArray = a;
        Arrays.sort(tempArray);

        for (int j : tempArray)
        {
            System.out.print(j + " ");
        }

        System.out.println();

        System.out.print("Enter the number you want to find: ");

        int toFind = sc.nextInt();

        boolean found = false;

        for(int i=0; i<n; i++)
        {
            if(a[i] == toFind)
            {
                found = true;
            }
        }

        if(found)
            System.out.println(toFind + " is found.");
        else
            System.out.println(toFind + " is not found.");
    }