以Java打印满足特定条件的数组如下所示

huangapple go评论67阅读模式
英文:

Print array with specific conditions as below in java

问题

数组问题 - int[] arr={1,2,3,4,5}

首先找出数组的中间元素

将数组的第一个元素添加到中间元素右侧最近的元素上,即 arr[0] + arr[3] = 1 + 4 = 5

然后将数组的第二个元素添加到中间元素右侧的下一个元素上,即 arr[1] + arr[4] = 2 + 5 = 7

打印这些和到中间元素之后,再打印中间元素之后的元素,输出为 [5,7,3,4,5]

注意:获取输出元素并将它们修改为原始数组的元素,这样如果我们打印原始数组,即

for (int i=0; i < arr.length; i++) {
    System.out.println(arr[i]); 
}

则输出必须为 '5 7 3 4 5'

我尝试过这个并获得了输出,但如何将输出转换为数组元素并填充到一个空数组中,并引用原始数组?

英文:

Array Question - int[] arr={1,2,3,4,5}

First find out middle element of array

Addition 1st element of array to right side closest element of middle element i.e arr[0] + arr[3] = 1 + 4 = 5

Again add 2nd element of array to next right side element of middle element i.e. arr[1] + arr[4] = 2 + 5 = 7

Print this sums up to middle element after middle element array should print elements as a original array and get output as [5,7,3,4,5]

Note: get output elements and modify them as a original array elements so if we print original array i.e.

for (int i=0; i < arr.length; i++) {
    System.out.println(arr[i]); 
}

then output must be '5 7 3 4 5'

I tried this and I got output but how can I convert output in array elements and fill it to an empty array and refer to original array

package javap;


public class Arraym {
    static void print (int arr[]) {
        int z = arr[arr.length / 2];

     	int sum1 = 0;
    	int sum2 = 0;
       
        for (int i = 0; i < arr.length/2; i++) {
     	    System.out.print(sum1= arr[i]+arr[i+z]) ;
        }
        for (int i = arr.length/2; i<arr.length; i++) {
            System.out.print(sum2=arr[i]);
        }
    } 

    public static void main(String[] args) {
    	int arr[]= {1,2,3,4,5};
	    print(arr);
    }
}

答案1

得分: -1

在Java中,数组是按引用传递的。因此,你可以直接修改它。

public class Arraym {
    static void print (int[] arr) {
        for (int i = 0; i < arr.length/2; i++) {
            int z = arr[arr.length/2];
            int sum1=arr[i]+arr[i+z];
            System.out.print(sum1);
            // 对数组的每个元素进行写入
            arr[i] = sum1;
        }
        
        for (int i = arr.length/2; i<arr.length; i++) {
              int sum2=arr[i];
              System.out.print(sum2);
              // 无需对数组进行写入,因为它已经包含了正确的值。
        }
    }
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        print(arr);
        
        System.out.println();
        for (int i : arr) {
            System.out.print(i); // 输出 57345
        }
    }   
}

注意:对程序进行了一些额外的编辑:

  1. 在Java中,推荐使用int[] variable而不是int variable[]。它们是一样的,但前者更好地描述了类型。
  2. 在函数开始时不需要声明所有变量。变量可以在函数的任何位置声明,并且只能在声明的块内可见。
  3. 发布前始终对代码进行缩进/格式化。
英文:

In Java arrays are passed by reference. Thus, you can just modify it directly.

public class Arraym {
    static void print (int[] arr) {
        for (int i = 0; i < arr.length/2; i++) {
            int z = arr[arr.length/2];
            int sum1=arr[i]+arr[i+z];
            System.out.print(sum1);
            // Write to each element of the array
            arr[i] = sum1;
        }
        
        for (int i = arr.length/2; i<arr.length; i++) {
              int sum2=arr[i];
              System.out.print(sum2);
              // No need to write to array, since it already contains the correct value.
        }
    }
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        print(arr);
        
        System.out.println();
        for (int i : arr) {
            System.out.print(i); // prints 57345
        }
    }   
}

Note: I've made a couple of extra edits to the program:

  1. In java, prefer int[] variable over int variable[]. They are the same, but the first is preferable as it better describes the type.
  2. There is no need to declare all variables at the start of the function. Variables can be declared anywhere in a function and will only be visible within the declared block.
  3. Always indent/format your code before posting.

huangapple
  • 本文由 发表于 2023年4月4日 13:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定