英文:
How to fill an array with elements from another array of different size? (leaving non existent indexes as zero)
问题
SCENARIO 1: 如果主数组长度小于8
声明:
int[] mainArray = new int[] { 1, 2, 3, 4, 5 }; // 至少没有元素要求
int[] arrayOne = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // 必须有8个元素
我想将mainArray中的值添加到arrayOne中,多余的元素将保留为零。
期望的数组:
int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 0, 0, 0 }; // 必须有8个元素
SCENARIO 2: 如果主数组长度大于8
声明:
int[] mainArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // 至少没有元素要求
int[] arrayOne = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // 必须有8个元素
int[] arrayTwo = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // 必须有8个元素
我想将mainArray中的前8个值添加到arrayOne中,然后将剩余的值添加到arrayTwo中,将其他索引保留为零(你会在第二个数组中看到9和10在右边,因此arrayOne从左到右,arrayTwo从右到左。如果有arrayThree,它将再次从左到右)
期望的数组:
int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // 必须有8个元素
int[] arrayTwo = new int[] { 0, 0, 0, 0, 0, 0, 10, 9 }; // 必须有8个元素
英文:
SCENARIO 1: if main array length is < 8
Declaration:
int[] mainArray = new int[] { 1, 2, 3, 4, 5 } // no minimum number of elements
int[] arrayOne = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // must have 8 elements
I want to add the values from mainArray into arrayOne, with the spare elements being left as zero.
Desired array:
int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 0, 0, 0 }; // must have 8 elements
SCENARIO 2: if main array length is > 8
Declaration:
int[] mainArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // no minimum number of elements
int[] arrayOne = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // must have 8 elements
int[] arrayTwo = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; // must have 8 elements
I want to add the the first 8 values from mainArray into arrayOne, and then the remaining values in arrayTwo, leaving the other indexes as zero (you'll see the 9 and 10 on the right of the second array, so arrayOne is left to right, arrayTwo is right to left. If there was an arrayThree, that would be left to right again)
Desired arrays:
int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // must have 8 elements
int[] arrayTwo = new int[] { 0, 0, 0, 0, 0, 0, 10, 9 }; // must have 8 elements
答案1
得分: 0
可以使用以下代码来处理:
int[] arr = new int[8];
Arrays.fill(arr, 0);
对于后续的数组,你可以从末尾索引开始,减去剩余条目数除以 8 的余数,以确定你需要填充多少其他数组。
英文:
You can use
Arrays.fill(arr, 0);
for your subsequent arrays, you can start from end index - length of remaining entries modulo 8 to arrive at how many other arrays you have to fill.
答案2
得分: 0
首先,我假设你之前展示的数组初始化是为了演示目的。如果不是的话,你可以像这样创建任何自定义数组:
int[] arrayOne = {n, n, n, n, n, n};
(在花括号之间用逗号分隔的任意数量的元素)
要实现你想要做的事情,你需要使用一系列循环。以下是我解决的方法:
如果(mainArray.length <= 8){
for(int i = 0; i < mainArray.length; i++){
arrayOne[i] = mainArray[i];
} // 用于复制和粘贴元素的非常标准的循环
} else {
int direction = 1; // 1表示从左到右,-1表示从右到左
int shortIndex = 0; // 当等于8时会修改,以回到索引零
for(int i = 0; i < mainArray.length; i++){
if(i + 1%8 == 0 && i!= 0){ // 当长度为8时切换方向
direction *= -1;
}
if(direction == 1){
arrayOne[shortIndex] = mainArray[i];
shortIndex++;
} else if(direction == -1){
arrayTwo[shortIndex] = mainArray[i];
shortIndex--;
}
}
}
这只是我草图般的基本思路,因为我实际上没有测试过这段代码,但我希望这能让你更好地理解如何解决这个问题。如果你只需要两个数组而不是三个或更多,这可能只能起作用。
基本上,如果mainArray的长度大于8,你需要一个额外的计数器变量,以及某种在arrayOne达到最大值并准备继续下一个数组时切换条件的变量。当这种情况发生时,我切换条件变量(direction),以便循环知道在数组之间如何迭代。
shortIndex是我提到的第二个计数器变量。它会跟随变量'i',在每次迭代中递增1,直到达到索引7(与长度8相同)。然后,一旦方向改变,它将从索引7开始(同样是长度8),并从那里开始递减。
希望这个解释不会太令人困惑,哈哈。
英文:
First off, I'm assuming that you made your Array initializations like that for the sake of the demonstration. If not, you can just create any custom array like this:
int[] arrayOne = {n, n, n, n, n, n};
(Using any number of elements separated by commas in between the curly brackets)
To achieve what you want to do, you would need to use a bunch of loops. This is how I would solve it:
if (mainArray.length <= 8) {
for (int i = 0; i < mainArray.length; i++) {
arrayOne[i] = mainArray[i];
} // very standard loop for copying and pasting elements
} else {
int direction = 1; // 1 means left to right, -1 means right to left
int shortIndex = 0; // modified as to return back to index zero when equal to 8
for (int i = 0; i < mainArray.length; i++) {
if (i + 1 % 8 == 0 && i != 0) { // switching directions when length 8 is reached
direction *= -1;
}
if (direction == 1) {
ArrayOne[shortIndex] = mainArray[i];
shortIndex++;
} else if (direction == -1) {
ArrayTwo[shortIndex] = mainArray[I];
shortIndex--;
}
}
}
This is really just me sketching out a basic idea as I haven't actually tested this code but I hope this gives you a better understanding of how you would solve it. Also this would probably only work if you only need two arrays as opposed to 3 or more.
Basically if the mainArray length is greater than 8 you will need an additional counter variable as well as some sort of variable that switches condition when arrayOne has hit its max and is ready to move on to the next one. When this happens, I switch the conditional variable (direction) so that my loop knows which direction to iterate through the arrays.
shortIndex is the second counter variable I was talking about. It will follow variable 'i' counting up by 1 each iteration until index 7 is reached (same as length 8), and then once the direction has changed it will begin at index 7 (again, same as length 8) and count down from there.
I hope this explanation wasn't too confusing lol
答案3
得分: 0
使用List<int[]>
来存储数组。它遍历mainArray
,按照指定的方式连续复制内容,直到没有剩余元素为止。它交替反转List
中的子列表。
mainArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14 }; // 没有最小元素数量限制
List<int[]> arrays = new ArrayList<>();
int size = 5;
int begin = 0;
// 将数组复制,向上舍入为大小的倍数,并用0值填充。
mainArray = Arrays.copyOf(mainArray, mainArray.length + (size - (mainArray.length % size)));
// 将mainArray转换为列表。需要这样做才能使用subList和reverse。
List<Integer> vals = IntStream.of(mainArray).boxed().collect(Collectors.toList());
boolean reverse = false;
while (begin < vals.size()) {
// 获取第一组数字,从左到右。
List<Integer> sublist = vals.subList(begin,begin+size);
// 检查是否需要反转子列表。
if (reverse) {
Collections.reverse(sublist);
}
// 现在只需将子列表复制到数组并存储起来。
arrays.add(sublist.stream().mapToInt(Integer::intValue).toArray());
// 更新状态
reverse = !reverse;
begin+=size;
}
for (int[] a : arrays) {
System.out.println(Arrays.toString(a));
}
对于size = 5
,打印结果为:
[1, 2, 3, 4, 5]
[10, 9, 8, 7, 6]
[11, 12, 13, 14, 0]
对于size = 8
,打印结果为:
[1, 2, 3, 4, 5, 6, 7, 8]
[0, 0, 14, 13, 12, 11, 10, 9]
以下是另一种只使用List
返回构造的数组的方法。它简单地在每隔一个迭代中改变填充方向。
public static List<int[]> fill(int[] main, int size) {
List<int[]> arrays = new ArrayList<>();
main = Arrays.copyOf(main,
main.length + (size - (main.length % size)));
int count = (main.length/size)+1;
int ainc = 0;
while (count-- > 1) {
int[] temp = new int[size];
arrays.add(temp);
boolean reverse = count % 2 == 0;
int idx = reverse ? size - 1 : 0;
int inc = reverse ? -1 : 1;
for (int i = 0; i < size; i++) {
temp[idx] = main[ainc++];
idx += inc;
}
}
return arrays;
}
英文:
For this I use a List<int[]>
to store the arrays. It iterates over the mainArray and successively copies the contents as specified until no more elements are left. It alternately reversed sublists of the List.
mainArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14 }; // no minimum number of elements
List<int[]> arrays = new ArrayList<>();
int size = 5;
int begin = 0;
// Copy the array, rounding up to a multiple of size, filling in with 0 value.
mainArray = Arrays.copyOf(mainArray, mainArray.length + (size - (mainArray.length % size)));
// convert the mainArray to a list. Need this to use subList and reverse.
List<Integer> vals = IntStream.of(mainArray).boxed().collect(Collectors.toList());
boolean reverse = false;
while (begin < vals.size()) {
// get the first group of numbers, going from left to right.
List<Integer> sublist = vals.subList(begin,begin+size);
// check if time to reverse the sublist.
if (reverse) {
Collections.reverse(sublist);
}
// now just copy the sublist to an array and store away.
arrays.add(sublist.stream().mapToInt(Integer::intValue).toArray());
// update state
reverse = !reverse;
begin+=size;
}
for (int[] a : arrays) {
System.out.println(Arrays.toString(a));
}
Prints for size = 5
[1, 2, 3, 4, 5]
[10, 9, 8, 7, 6]
[11, 12, 13, 14, 0]
For size = 8
[1, 2, 3, 4, 5, 6, 7, 8]
[0, 0, 14, 13, 12, 11, 10, 9]
Here's another method that only uses a List to return the constructed arrays. It simply changes the fill direction every other iteration.
public static List<int[]> fill(int[] main, int size) {
List<int[]> arrays = new ArrayList<>();
main = Arrays.copyOf(main,
main.length + (size - (main.length % size)));
int count = (main.length/size)+1;
int ainc = 0;
while (count-- > 1) {
int[] temp = new int[size];
arrays.add(temp);
boolean reverse = count % 2 == 0;
int idx = reverse ? size - 1 : 0;
int inc = reverse ? -1 : 1;
for (int i = 0; i < size; i++) {
temp[idx] = main[ainc++];
idx += inc;
}
}
return arrays;
}
答案4
得分: 0
试一试。
static int copy(int[] mainArray, int start, int[] array) {
int mainArrayLength = mainArray.length;
if (start >= mainArrayLength) return start;
int length = Math.min(mainArrayLength - start, array.length);
System.arraycopy(mainArray, start, array, 0, length);
return start + length;
}
static void reverse(int[] array) {
for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
static void copy(int[] mainArray, int[] arrayOne, int[] arrayTwo) {
int start = copy(mainArray, 0, arrayOne);
copy(mainArray, start, arrayTwo);
reverse(arrayTwo);
}
和
int[] mainArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arrayOne = {0, 0, 0, 0, 0, 0, 0, 0};
int[] arrayTwo = {0, 0, 0, 0, 0, 0, 0, 0};
copy(mainArray, arrayOne, arrayTwo);
System.out.println("arrayOne = " + Arrays.toString(arrayOne));
System.out.println("arrayTwo = " + Arrays.toString(arrayTwo));
输出
arrayOne = [1, 2, 3, 4, 5, 6, 7, 8]
arrayTwo = [0, 0, 0, 0, 0, 0, 10, 9]
如果你想要复制到超过两个数组而不反转它们,可以这样做。
static void copy(int[] mainArray, int[]... arrays) {
int start = 0;
for (int[] array : arrays)
start = copy(mainArray, start, array);
}
和
int[] mainArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int[] arrayOne = {0, 0, 0, 0, 0};
int[] arrayTwo = {0, 0, 0, 0, 0};
int[] arrayThree = {0, 0, 0, 0, 0};
copy(mainArray, arrayOne, arrayTwo, arrayThree);
System.out.println("arrayOne = " + Arrays.toString(arrayOne));
System.out.println("arrayTwo = " + Arrays.toString(arrayTwo));
System.out.println("arrayThree = " + Arrays.toString(arrayThree));
输出
arrayOne = [1, 2, 3, 4, 5]
arrayTwo = [6, 7, 8, 9, 10]
arrayThree = [11, 12, 13, 0, 0]
英文:
Try this.
static int copy(int[] mainArray, int start, int[] array) {
int mainArrayLength = mainArray.length;
if (start >= mainArrayLength) return start;
int length = Math.min(mainArrayLength - start, array.length);
System.arraycopy(mainArray, start, array, 0, length);
return start + length;
}
static void reverse(int[] array) {
for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
static void copy(int[] mainArray, int[] arrayOne, int[] arrayTwo) {
int start = copy(mainArray, 0, arrayOne);
copy(mainArray, start, arrayTwo);
reverse(arrayTwo);
}
and
int[] mainArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arrayOne = {0, 0, 0, 0, 0, 0, 0, 0};
int[] arrayTwo = {0, 0, 0, 0, 0, 0, 0, 0};
copy(mainArray, arrayOne, arrayTwo);
System.out.println("arrayOne = " + Arrays.toString(arrayOne));
System.out.println("arrayTwo = " + Arrays.toString(arrayTwo));
output
arrayOne = [1, 2, 3, 4, 5, 6, 7, 8]
arrayTwo = [0, 0, 0, 0, 0, 0, 10, 9]
Do this if you want to copy to more than two arrays without reversing them.
static void copy(int[] mainArray, int[]... arrays) {
int start = 0;
for (int[] array : arrays)
start = copy(mainArray, start, array);
}
and
int[] mainArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int[] arrayOne = {0, 0, 0, 0, 0};
int[] arrayTwo = {0, 0, 0, 0, 0};
int[] arrayThree = {0, 0, 0, 0, 0};
copy(mainArray, arrayOne, arrayTwo, arrayThree);
System.out.println("arrayOne = " + Arrays.toString(arrayOne));
System.out.println("arrayTwo = " + Arrays.toString(arrayTwo));
System.out.println("arrayThree = " + Arrays.toString(arrayThree));
output
arrayOne = [1, 2, 3, 4, 5]
arrayTwo = [6, 7, 8, 9, 10]
arrayThree = [11, 12, 13, 0, 0]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论