Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我有一个数组程序,条件是必须返回一个新数组,替换数组中下一个最高的元素_Java_Arrays - Fatal编程技术网

Java 我有一个数组程序,条件是必须返回一个新数组,替换数组中下一个最高的元素

Java 我有一个数组程序,条件是必须返回一个新数组,替换数组中下一个最高的元素,java,arrays,Java,Arrays,假设我有一个输入[10,8,6,15,2,-1] 输出应为[15,10,8,15,6,2] 我已经编写了一套代码: public static void main(String[] args) { int[] unsortesArray=new int[]{10,8,6,15,2,-1}; int len=unsortesArray.length; for(int i=0;i<len; i++){ for(i

假设我有一个输入[10,8,6,15,2,-1]

输出应为[15,10,8,15,6,2]

我已经编写了一套代码:

    public static void main(String[] args) {
        int[] unsortesArray=new int[]{10,8,6,15,2,-1};
        int len=unsortesArray.length;

        for(int i=0;i<len; i++){
            for(int j=0; j<len; j++){
                if(unsortesArray[i]<unsortesArray[j]){
                    unsortesArray[i]=unsortesArray[j];
                }
            }
            System.out.println(unsortesArray[i]);
        }
    }
publicstaticvoidmain(字符串[]args){
int[]unsortsarray=新的int[]{10,8,6,15,2,-1};
int len=unsortsarray.length;

对于(int i=0;i您需要交换两个数字:

public static void main(String[] args) {
    int[] unsortesArray=new int[]{10,8,6,15,2,-1};
    int len=unsortesArray.length;

    for(int i=0;i<len; i++){
        for(int j=i+1; j<len; j++){
            if(unsortesArray[i]<unsortesArray[j]){
                int temp = unsortesArray[i]; // create a temp var to store the value you are going to swap 
                unsortesArray[i]=unsortesArray[j]; // swap the value
                unsortesArray[j] = temp; // save it back again in the array 
            }
        }
        System.out.println(unsortesArray[i]);
    }
}
publicstaticvoidmain(字符串[]args){
int[]unsortsarray=新的int[]{10,8,6,15,2,-1};
int len=unsortsarray.length;

对于(int i=0;我能请您在所需的输出上添加逻辑吗?我期望的o/p不是来自正确的sun数组最高元素,它只是从第i个元素到整个数组的最高值。嘿@Rahula,您的问题不清楚,请您详细说明。是的问题陈述是,假设我们有一个[10,8,6,15,2,-1]的数组现在我想要另一个数组作为一个输出,它将有一个最大的数字来代替数组中的元素。假设从输入来看,o/p应该是[15,10,8,15,6,2],因为10的最大值是15,8的最大值是10而不是15,6的最大值是8而不是10或15。