英文:
Use Arrays when initializing another Array at specific position
问题
我有一个整数数组。
int[] arr1 = new int[] {3, 4, 5};
。我想在初始化另一个数组时插入这个数组。这样当
int[] arr2 = new int[] {8, 7, arr1, 1, 0};
,
arr2将会等于{8, 7, 3, 4, 5, 1, 0}
。
另外,我不知道arr1
的长度,因此无法这样做:int[] arr2 = new int[] {8, 7, arr1[0], arr1[1], arr1[2], 1, 0};
非常感谢任何帮助。
英文:
I have an integer array.
int[] arr1 = new int[] {3, 4, 5};
. I want to insert this array when initializing another array. So that when
int[] arr2 = new int[] {8, 7, arr1, 1, 0};
, arr2 will equal {8, 7, 3, 4, 5, 1, 0}
.
Also, I do not know the length of arr1
, so I can't do int[] arr2 = new int[] {8, 7, arr1[0], arr1[1], arr1[2], 1, 0};
Any help is greatly appreciated.
答案1
得分: 4
很遗憾,据我所知,这样的操作无法在单个操作中完成。我能想到的最好办法是创建一个 result
数组。然后你先将 arr2
的第一部分复制到其中,然后将整个 arr1
复制进去,最后再复制 arr2
的第二部分。最终的方法看起来是这样的:
private static int[] insertArrayAtPosition(int[] arr1, int[] arr2, int insertPos){
int[] result = new int[arr1.length + arr2.length];
System.arraycopy(arr2, 0, result, 0, insertPos);
System.arraycopy(arr1, 0, result, insertPos, arr1.length);
System.arraycopy(arr2, insertPos, result, insertPos + arr1.length, arr2.length - insertPos);
return result;
}
然后你可以这样调用它:
public static void main(String args[]) {
int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[] {8, 7, 1, 0};
int[] result = insertArrayAtPosition(arr1, arr2, 2);
System.out.println(Arrays.toString(result));
}
编辑:Milgo 的解决方案看起来好多了。然而,我的解决方案允许在已创建数组之后,在特定位置将一个数组插入另一个数组。根据用例选择使用哪种方法。
英文:
Sadly as far as I'm aware something like that can't be done in a single operation. The best I was able to come up with is to create a result
array. Then you copy to it first part of arr2
, whole arr1
and finally second part of arr2
. In the end whole method would look like so:
private static int[] insertArrayAtPosition(int[] arr1, int[] arr2, int insertPos){
int[] result = new int[arr1.length + arr2.length];
System.arraycopy(arr2, 0, result, 0, insertPos);
System.arraycopy(arr1, 0, result, insertPos, arr1.length);
System.arraycopy(arr2, insertPos, result, insertPos + arr1.length, arr2.length - insertPos);
return result;
}
And then you can call it like so:
public static void main(String args[]) {
int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[] {8, 7, 1, 0};
int[] result = insertArrayAtPosition(arr1, arr2, 2);
System.out.println(Arrays.toString(result));
}
Edit: Milgo's solution looks much better. However, mine allows to insert one array into another at specific position after they are already created. Which one to use depeds on the usecase.
答案2
得分: 1
你可以使用ArrayUtils.addAll()
三次。
int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[arr1.length + 4];
ArrayUtils.addAll(arr2, new int[]{8, 7});
ArrayUtils.addAll(arr2, arr1);
ArrayUtils.addAll(arr2, new int[]{1, 0});
ArrayUtils
是Apache Commons的一部分。
英文:
You could use ArrayUtils.addAll()
three times.
int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[arr1.length + 4];
ArrayUtils.addAll(arr2, new int[]{8, 7});
ArrayUtils.addAll(arr2, arr1);
ArrayUtils.addAll(arr2, new int[]{1, 0});
ArrayUtils is part of Apache Commons.
答案3
得分: 0
这在Java中不受原生支持/没有语言功能允许您使用一行代码。您当然可以编写一个循环来插入另一个数组的元素,还可以使用列表接口或Arrays#stream。可能最好的方法是使用列表,像这样:
List<Integer> arr1 = Arrays.asList(3, 4, 5);
List<Integer> arr2 = Arrays.asList(8, 7, 1, 0);
arr2.addAll(2, arr1);
希望这有所帮助。
编辑:顺便说一下,这实际上是一行代码
英文:
This is not natively supported in java / there is no language feature that allows you to use a one liner.
You can of course write a loop that inserts the elements of the other array, you can also use list interfaces or Arrays#stream. The best way to do it should probably be to use lists, like this:
List<Integer> arr1 = Arrays.asList(3, 4, 5);
List<Integer> arr2 = Arrays.asList(8, 7, 1, 0);
arr2.addAll(2, arr1);
Hope this helpes.
Edit: It is a one liner btw
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论