英文:
Java Hackerank left array Rotation
问题
以下是翻译后的代码部分:
static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];  // 看这里,为什么这样可以工作
        while(logicBreak < a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }
        logicBreak = 0;
    
        while(logicBreak < d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }
        return copy;
    }
希望这能满足您的需求。
英文:
We have a method in which an array and number of rotations are passed as input and we have to return the array after left rotations. Here is my solution.
static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];  // SEE HERE GUYS, WHY THIS WORKS
        int[] copy = a;  // AND WHY NOT THIS,
        while(logicBreak < a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }
        logicBreak = 0;
    
        while(logicBreak < d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }
        return copy;
    }
I hope that there is no confusion until now and if you have then, open this page. So my problem is really basic. Why the answer works when I do this  int[] copy = new int[a.length]; but does not work when I do this int[] copy = a;. Can you tell me the difference, because we are changing all the values of copy array, so what is the matter we make it new int[] or same as the array a. I have created an android app but I am still not getting this array concept, if there is a difference then please tell me.
For your ease here is the full code.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];
        while(logicBreak < a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }
        logicBreak = 0;
    
        while(logicBreak < d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }
        return copy;
    }
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        String[] nd = scanner.nextLine().split(" ");
        int n = Integer.parseInt(nd[0]);
        int d = Integer.parseInt(nd[1]);
        int[] a = new int[n];
        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }
        int[] result = rotLeft(a, d);
        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));
            if (i != result.length - 1) {
                bufferedWriter.write(" ");
            }
        }
        bufferedWriter.newLine();
        bufferedWriter.close();
        scanner.close();
    }
}
答案1
得分: 2
当你执行以下操作时:
int[] copy = a;
你只是将 a 的引用赋值给了 copy。它们在内存中"引用"同一个位置。
以下是一个示例:
int[] a = {1,2,3};
int[] copy  = a;
a[1] = 100;
System.out.println(Arrays.toString(copy));
输出结果为:
[1,100,3]
英文:
When you do the following:
int[] copy = a;
You are just assigning the reference of a to copy.  They "refer" to the same location in memory.
Here is a demo
int[] a = {1,2,3};
int[] copy  = a;
a[1] = 100;
System.out.println(Arrays.toString(copy));
Prints
[1,100,3]
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论