gpt4 book ai didi

java - 将原数组中的值替换为新数组

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:39 24 4
gpt4 key购买 nike

我有一个界面 -

//INTERFACE -
public interface IntSequence {
int length();
int get(int index);
void set(int index, int value);
/**
* Returns a contiguous subsequence of size "size" which starts from
* the index "index" and is backed by the sequence;
* that is, changing it through {@link IntSequence#set(int, int)}
* affects the original sequence as well.
* @param index the starting position of the subsequence
* @param size the subsequence size
* @return a sequence of ints
*/
IntSequence subSequence(int index, int size);
}

以及一个实现它的类 -

public class IntArray implements IntSequence {

int[] a;
static int test;
static int[] b;
static int[] c;
int[] d;
int use;
int j;
int[] mama;
int[] mama2;
int indexgeter;

public IntArray(int size) {
j = size;
a = new int[size];
b = new int[size];
a = b;
}

public IntArray(int index, int size, int[] array) {
this.a = array;
int counter = 0;
while(counter < size) {
array[counter] = array[index];
counter++;
index++;
}
}

@Override
public int length() {
return a.length;
}

@Override
public int get(int index) {
return a[index];
}

@Override
public void set(int index, int value) {
a[index] = value;
}

@Override
public IntSequence subSequence(int index, int size) {
IntSequence resultseq = new IntArray(index, size, a);
return resultseq;
}
public static void main(String[] args) {
IntSequence a = new IntArray(5);
a.set(0, 0);
a.set(1, 10);
a.set(2, 20);
a.set(3, 30);
a.set(4, 40);

System.out.println("Initial array");
System.out.println("size: " + a.length());
for (int i = 0; i < 5; ++i)
System.out.println("a[" + i + "]: " + a.get(i));

System.out.println("Creating subarray (2, 2)");
IntSequence s = a.subSequence(2, 2);
System.out.println("s.size: " + s.length());

System.out.println("Multiplying subarray's last element");
s.set(1, s.get(1) * 10);
System.out.println("Subarray after modification:");
for (int i = 0; i < s.length(); ++i)
System.out.println("s[" + i + "]: " + s.get(i));

System.out.println("Array after modification:");
for (int i = 0; i < 5; ++i)
System.out.println("a[" + i + "]: " + a.get(i));

a.subSequence(0, 1).subSequence(0, 1).subSequence(0, 1).set(0, -10);
System.out.println("First element changed to: " + a.get(0));
}
}

问题 - 在这里,我想要做的就是返回一个使用方法 IntSequence subSequence(int index, int size) 创建的子数组。但是,我的代码在运行时所做的是返回以下输出 -

Initial array
size: 5
a[0]: 0
a[1]: 10
a[2]: 20
a[3]: 30
a[4]: 40
Creating subarray (2, 2)
s.size: 5
Multiplying subarray's last element
Subarray after modification:
s[0]: 20
s[1]: 300
s[2]: 20
s[3]: 30
s[4]: 40
Array after modification:
a[0]: 20
a[1]: 300
a[2]: 20
a[3]: 30
a[4]: 40
First element changed to: -10

然而,这是预期的输出 -

Initial array
size: 5
a[0]: 0
a[1]: 10
a[2]: 20
a[3]: 30
a[4]: 40
Creating subarray (2, 2)
s.size: 2
Multiplying subarray's last element
Subarray after modification:
s[0]: 20
s[1]: 300
Array after modification:
a[0]: 0
a[1]: 10
a[2]: 20
a[3]: 300
a[4]: 40
First element changed to: -10

ISSUE - 正如您从上面的预期输出中看到的,当我得到一个子数组时,索引处的原始数组 (a) 的值 - 索引也应该更新为该值转换为子数组。

例如 -

int[] a = new int[] {1,2,3,4,5}; //we have this original array over here
//You use subSequence on it
a.subSequence(2,2);
//Now the elements you will have will be
s[0] = a[2]; //Which will be 3 in this case
s[1] = a[3]; //Which will be 4 in this case

//You make some changes on s[0]
s[0] * 10;
s[1] * 100;

//The original array whose indexes s referred to should be modified aswell now cause you did some changes to the subarray
//The new array will be
a[0] = 1;
a[1] = 2;
a[2] = 30;
a[3] = 400;
a[4] = 5;

我知道这可能是制作给出预期输出的代码的最糟糕方法,但我不允许制作一种立即具有整数数组返回类型的方法以达到目的。几天来我一直在想办法找到一种方法来完成需要的事情。

非常感谢您花时间查看!

最佳答案

为了实现

affects the original sequence as well.

这是最具挑战性的部分,您需要将整数包装到对象中并在实现中存储这些包装对象的数组或列表。通过这种方式,更改此包装器中的内部 int 将影响您的所有序列,因为通过存储包装器,您将仅存储对这些包装器对象的引用,而不是像 int 那样存储对象本身。

它是如何工作的:

这都是关于 java 中对象与原始类型的引用。在基本类型 a = b 的情况下,实际上在 a 中创建 b 的副本,在对象 a = b 的情况下,仅将对象 b 的引用复制到 a 中。因此,更改 b 的内部结构将影响您通过引用 a 看到的内容。

关于java - 将原数组中的值替换为新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com