首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从its循环中重用数组的元素?

如何从its循环中重用数组的元素?
EN

Stack Overflow用户
提问于 2021-12-14 17:19:48
回答 3查看 74关注 0票数 0

我试图创建一个代码,该代码按照用户的升序在数组-by中重新排列给定的元素,我已经这样做了,但是程序需要在排序之后打印给定的元素,然后在排序之前打印它们。我对在排序后打印元素没有问题。

问题是在排序之前打印它们,如何将给定的元素由用户重新使用,以使其退出for循环。

代码语言:javascript
复制
import java.util.*;

public class SortingnumbersANDswapping {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int swap;  
        int ar[] = new int[3]; //{8,10,5}
        for (int S = 0; S < ar.length; S++) {
            ar[S] = in.nextInt();    //this for loop is used to store numbers in the array

        }
 
 
        for (int i = 0; i < ar.length; i++) {
            /* this nested for loop is used to compare the first element with the second one in the array
              or the second element with the third.
             */
            for (int j = i + 1; j < ar.length; j++) {
                if (ar[i] > ar[j]) {           //8>10-->F  ,  8>5 -->T ,     {5,10,8}  the new arrangment we are going to use
                    swap = ar[i];             // 10>8-->T               {5,8,10}
                    ar[i] = ar[j];
                    ar[j] = swap;

                }
            }

            System.out.println(ar[i]);  // to print the new order print it inside the array
            
        }
              
        // I wanna do something like that
        // System.out.println(ar[S]);
        // but of course I cant cause array S is only defined in it's loop       
    }

}
EN

回答 3

Stack Overflow用户

发布于 2021-12-14 19:52:52

您不能重用它,您需要将原始数组保存在另一个保持不变的数组中。此外,在Java中,数组通常被声明为int[] ar,而不是int ar[]。以下内容应按预期的方式发挥作用:

代码语言:javascript
复制
public class SortingnumbersANDswapping {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int swap;
        int[] ar = new int[3]; //{8,10,5}
        for (int S = 0; S < ar.length; S++) {
            ar[S] = in.nextInt();    //this for loop is used to store numbers in the array
        }

        System.out.println("::: Original Array :::");
        int[] originalArray = Arrays.copyOf(ar, ar.length);
        for (int j : originalArray) {
            System.out.println(j);
        }

        System.out.println("::: Sorted Array :::");
        for (int i = 0; i < ar.length; i++) {
            for (int j = i + 1; j < ar.length; j++) {
                if (ar[i] > ar[j]) {           //8>10-->F  ,  8>5 -->T ,     {5,10,8}  the new arrangment we are going to use
                    swap = ar[i];             // 10>8-->T               {5,8,10}
                    ar[i] = ar[j];
                    ar[j] = swap;

                }
            }
            System.out.println(ar[i]);  // to print the new order print it inside the array
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-12-14 20:02:50

在开始更改数组之前,可以使用数组库( copyOf )中的java.util.Arrays方法复制数组。在这里您可以找到一些不同的方法-- https://www.softwaretestinghelp.com/java-copy-array/amp/

票数 0
EN

Stack Overflow用户

发布于 2021-12-14 20:16:23

可以使用System.out.print(Arrays.toString(arr));打印整个数组。

代码语言:javascript
复制
public static void main(String... args) {
    int[] arr = readArray(3);
    
    System.out.print(Arrays.toString(arr));
    
    for (int i = 0; i < arr.length; i++) {
        for (int j = i + 1; j < ar.length; j++) {
            if (arr[i] > arr[j]) {
                swap(arr, i, j);
                System.out.print(Arrays.toString(arr));
            }
        }
    }
}

private static int[] readArray(int size) {
    System.out.format("Enter %d elements:\n", size);
    
    Scanner scan = new Scanner(System.in);
    int[] arr = new int[size];
    
    for(itn i = 0; i < arr.length; i++)
        arr[i] = scan.nextInt();
    
    return arr;
}

private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70353028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档