如何用另一个不同大小的数组中的元素填充一个数组? (将不存在的索引保留为零)
How to fill an array with elements from another array of different size? (leaving non existent indexes as zero)
场景 1:如果主数组长度小于 8
声明:
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
我想将 mainArray 中的值添加到 arrayOne 中,备用元素保留为零。
所需数组:
int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 0, 0, 0 }; // must have 8 elements
场景 2:如果主数组长度 > 8
声明:
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
我想将 mainArray 中的前 8 个值添加到 arrayOne 中,然后将剩余的值添加到 arrayTwo 中,将其他索引保留为零(您会在第二个数组的右侧看到 9 和 10,所以arrayOne是从左到右,arrayTwo是从右到左。如果有arrayThree,那将再次从左到右)
所需数组:
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
您可以使用
Arrays.fill(arr, 0);
对于您的后续数组,您可以从结束索引开始 - 剩余条目的长度模 8 以得出您必须填充的其他数组的数量。
首先,我假设您为了演示而进行了数组初始化。如果没有,您可以像这样创建任何自定义数组:
int[] arrayOne = {n, n, n, n, n, n};
(在大括号之间使用逗号分隔的任意数量的元素)
要实现你想做的事情,你需要使用一堆循环。这就是我要解决的方法:
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--;
}
}
}
这实际上只是我勾勒出的一个基本想法,因为我还没有实际测试过这段代码,但我希望这能让你更好地理解你将如何解决它。此外,这可能仅在您只需要两个数组而不是 3 个或更多数组时才有效。
基本上,如果 mainArray 长度大于 8,您将需要一个额外的计数器变量以及某种变量,当 arrayOne 达到其最大值并准备好移动到下一个时,它会切换条件。发生这种情况时,我切换条件变量(方向),以便我的循环知道在哪个方向遍历数组。
shortIndex 是我所说的第二个计数器变量。它将跟随变量 'i' 每次迭代递增 1,直到达到索引 7(与长度 8 相同),然后一旦方向改变,它将从索引 7 开始(同样,与长度 8 相同)并计数从那里下来。
我希望这个解释不会太混乱哈哈
为此,我使用 List<int[]>
来存储数组。它遍历 mainArray 并连续复制指定的内容,直到不再有元素为止。它交替反转列表的子列表。
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));
}
打印尺寸 = 5
[1, 2, 3, 4, 5]
[10, 9, 8, 7, 6]
[11, 12, 13, 14, 0]
尺码 = 8
[1, 2, 3, 4, 5, 6, 7, 8]
[0, 0, 14, 13, 12, 11, 10, 9]
这是另一种方法,它仅使用 List 来 return 构造的数组。它只是每隔一次迭代更改一次填充方向。
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;
}
试试这个。
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]
场景 1:如果主数组长度小于 8
声明:
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
我想将 mainArray 中的值添加到 arrayOne 中,备用元素保留为零。
所需数组:
int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 0, 0, 0 }; // must have 8 elements
场景 2:如果主数组长度 > 8
声明:
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
我想将 mainArray 中的前 8 个值添加到 arrayOne 中,然后将剩余的值添加到 arrayTwo 中,将其他索引保留为零(您会在第二个数组的右侧看到 9 和 10,所以arrayOne是从左到右,arrayTwo是从右到左。如果有arrayThree,那将再次从左到右)
所需数组:
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
您可以使用
Arrays.fill(arr, 0);
对于您的后续数组,您可以从结束索引开始 - 剩余条目的长度模 8 以得出您必须填充的其他数组的数量。
首先,我假设您为了演示而进行了数组初始化。如果没有,您可以像这样创建任何自定义数组: int[] arrayOne = {n, n, n, n, n, n}; (在大括号之间使用逗号分隔的任意数量的元素)
要实现你想做的事情,你需要使用一堆循环。这就是我要解决的方法:
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--;
}
}
}
这实际上只是我勾勒出的一个基本想法,因为我还没有实际测试过这段代码,但我希望这能让你更好地理解你将如何解决它。此外,这可能仅在您只需要两个数组而不是 3 个或更多数组时才有效。
基本上,如果 mainArray 长度大于 8,您将需要一个额外的计数器变量以及某种变量,当 arrayOne 达到其最大值并准备好移动到下一个时,它会切换条件。发生这种情况时,我切换条件变量(方向),以便我的循环知道在哪个方向遍历数组。
shortIndex 是我所说的第二个计数器变量。它将跟随变量 'i' 每次迭代递增 1,直到达到索引 7(与长度 8 相同),然后一旦方向改变,它将从索引 7 开始(同样,与长度 8 相同)并计数从那里下来。
我希望这个解释不会太混乱哈哈
为此,我使用 List<int[]>
来存储数组。它遍历 mainArray 并连续复制指定的内容,直到不再有元素为止。它交替反转列表的子列表。
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));
}
打印尺寸 = 5
[1, 2, 3, 4, 5]
[10, 9, 8, 7, 6]
[11, 12, 13, 14, 0]
尺码 = 8
[1, 2, 3, 4, 5, 6, 7, 8]
[0, 0, 14, 13, 12, 11, 10, 9]
这是另一种方法,它仅使用 List 来 return 构造的数组。它只是每隔一次迭代更改一次填充方向。
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;
}
试试这个。
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]