英文:
Combing two arrays in ascending order with unknown values inside the array
问题
我已成功创建了两个数组,但我无法弄清楚如何将这两个数组合并。我看到的每个教程都是这样合并它们的:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// 输出:3, 4, 5, 6, 8, 9
我需要的是能够输出:3, 3, 3, 4, 5, 5, 6, 6, 8, 9。以下是我目前编写的代码:
import java.util.Scanner;
public class Merger {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] arr1 = new int[10000];
int[] arr2 = new int[10000];
int[] merged = new int[20000];
int a1 = 0;
int a2 = 0;
int size = -1;
int size2 = -1;
int sizecheck = 0;
int size2check = 0;
System.out.println("输入第一个数组的值,最多10,000个值,输入负数以退出");
for (int i = 0; i < arr1.length; i++) {
arr1[i] = scan.nextInt();
merged[i] = arr1[i];
if (arr1[i] <= 0) {
break;
}
if (size <= arr1[i]) {
size = arr1[i];
sizecheck++;
}
a1++;
}
System.out.println("输入第二个数组的值,最多10,000个值,输入负数以退出");
for (int i = 0; i < arr2.length; i++) {
arr2[i] = scan.nextInt();
merged[i + a1] = arr2[i];
if (arr2[i] <= 0) {
break;
}
if (size2 <= arr2[i]) {
size2 = arr2[i];
size2check++;
}
a2++;
}
System.out.println("第一个数组:");
for (int i = 0; i < a1; i++) {
System.out.print(" " + arr1[i]);
}
System.out.println("\n第二个数组:");
for (int i = 0; i < a2; i++) {
System.out.print(" " + arr2[i]);
}
}
}
这会打印出两个数组,但不会将它们合并并排序。
英文:
I have managed to create both arrays, however I can't figure out how to combine the two arrays. Every tutorial I see merges them as such:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// Output: 3, 4, 5, 6, 8, 9
What I need is something that would output: 3, 3, 3, 4, 5, 5, 6, 6, 8, 9
Here is the code I have written so far:
import java.util.Scanner;
public class Merger {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] arr1 = new int[10000];
int[] arr2 = new int[10000];
int[] merged = new int[20000];
int a1 = 0;
int a2 = 0;
int size = -1;
int size2 = -1;
int sizecheck = 0;
int size2check = 0;
System.out
.println("Enter the values for the first array, up to 10,000 values, enter a negative number to quit");
for (int i = 0; i < arr1.length; i++) {
arr1[i] = scan.nextInt();
merged[i] = arr1[i];
if (arr1[i] <= 0) {
break;
}
if (size <= arr1[i]) {
size = arr1[i];
sizecheck++;
}
a1++;
}
System.out
.println("Enter the values for the second array, up to 10,000 values, enter a negative number to quit");
for (int i = 0; i < arr2.length; i++) {
arr2[i] = scan.nextInt();
merged[i + a1] = arr2[i];
if (arr2[i] <= 0) {
break;
}
if (size2 <= arr2[i]) {
size2 = arr2[i];
size2check++;
}
a2++;
}
System.out.println("First Array: ");
for (int i = 0; i < a1; i++) {
System.out.print(" " + arr1[i]);
}
System.out.println("\nSecond Array: ");
for (int i = 0; i < a2; i++) {
System.out.print(" " + arr2[i]);
}
}
}
This prints both arrays out, however does not combine and sort the two.
答案1
得分: 3
以下是代码的翻译部分:
这是代码!可能有更快/更简单的方法来做这个,但只要这两个数组是排序的,这个方法就有效。
public static void main(String[] args) {
int[] a1 = {1, 2, 3, 5};
int[] a2 = {1, 3, 4, 4, 4, 5};
int[] a3 = merge(a1, a2);
for (int i : a3) {
System.out.print(i);
}
}
public static int[] merge(int[] a1, int[] a2) {
int[] a3 = new int[a1.length + a2.length];
int indexA1 = 0;
int indexA2 = 0;
for (int i = 0; i < a3.length; i++) {
int n;
if (indexA1 == a1.length && indexA2 < a2.length) {
n = a2[indexA2];
indexA2++;
} else if (indexA1 < a1.length && indexA2 == a2.length) {
n = a1[indexA1];
indexA1++;
} else {
if (a1[indexA1] < a2[indexA2]) {
n = a1[indexA1];
indexA1++;
} else {
n = a2[indexA2];
indexA2++;
}
}
a3[i] = n;
}
return a3;
}
英文:
Here's the code ! There may be a faster/easier way to do it but this one works as long as the 2 arrays are sorted
public static void main(String[] args) {
int[] a1 = {1, 2, 3, 5};
int[] a2 = {1, 3, 4, 4, 4, 5};
int[] a3 = merge(a1, a2);
for (int i : a3) {
System.out.print(i);
}
}
public static int[] merge(int[] a1, int[] a2) {
int[] a3 = new int[a1.length + a2.length];
int indexA1 = 0;
int indexA2 = 0;
for (int i = 0; i < a3.length; i++) {
int n;
if (indexA1 == a1.length && indexA2 < a2.length) {
n = a2[indexA2];
indexA2++;
} else if (indexA1 < a1.length && indexA2 == a2.length) {
n = a1[indexA1];
indexA1++;
} else {
if (a1[indexA1] < a2[indexA2]) {
n = a1[indexA1];
indexA1++;
} else {
n = a2[indexA2];
indexA2++;
}
}
a3[i] = n;
}
return a3;
}
答案2
得分: 0
我假设您对流(Streams)还不太熟悉,但我想给您举个例子,说明您可以用它们做些什么。
添加导入语句:
import java.util.stream.IntStream;
在您的 main
方法末尾添加以下内容:
System.out.println("");
IntStream arr1Stream = IntStream.of(arr1).limit(a1); // 使用前 a1 个 arr1 值创建一个 IntStream
IntStream arr2Stream = IntStream.of(arr2).limit(a2);
int[] both = IntStream.concat(arr1Stream, arr2Stream).sorted().toArray(); // 合并这两个流,对它们进行排序并转换为数组
System.out.println(Arrays.toString(both)); // 打印数组的简便方式
注意:由于您要求只返回翻译好的部分,我已删除了注释。如有需要,您可以根据原始文本添加注释。
英文:
I assume that you are not yet familiar with Streams, but I would like to give you an example of what you can do with them.
Add import
import java.util.stream.IntStream;
Add this at the end of your main method
System.out.println("");
IntStream arr1Stream = IntStream.of(arr1).limit(a1); //creates an InStream with the first a1 values of arr1
IntStream arr2Stream = IntStream.of(arr2).limit(a2);
int[] both = IntStream.concat(arr1Stream, arr2Stream).sorted().toArray(); //combines the two streams, sorts them an converts them to an Array
System.out.println(Arrays.toString(both)); //easy way to print an array
答案3
得分: 0
以下是翻译好的部分:
使用流(Stream)的最简单方法如下:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// 将两个数组转换为流
// 将它们扁平化为单个 IntStream
// 对它们进行排序
// 转换为数组
int[] combined = Stream.of(arr1, arr2)
.flatMapToInt(Arrays::stream)
.sorted()
.toArray();
System.out.println(Arrays.toString(combined));
输出结果为:
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
非流式的方法可以如下实现:
// 扩展 arr1 以容纳 arr2
int oldLen = arr1.length;
arr1 = Arrays.copyOf(arr1, arr1.length + arr2.length);
// 将 arr2 从 0 处复制到 arr1 从旧长度位置开始
// 复制长度为 arr2 的长度
System.arraycopy(arr2, 0, arr1, oldLen, arr2.length);
// 排序并打印
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
输出结果为:
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
虽然根据您的问题描述,并没有提到合并已排序的数组,以下是如何执行该操作的方法。
这个算法很简单。只需遍历每个数组并比较当前值。
- 如果
arr1[i] <= arr2[k]
,则将arr1[i]
复制到结果中,将i
增加1
- 否则将
arr2[k]
复制到结果中,将k
增加1
。 - 在所有情况下,结果索引
r
都会增加1
public int[] merge(int[] arr1, int[] arr2) {
// 结果数组
int[] result = new int[arr1.length + arr2.length];
int r = 0;
int k = 0;
int i = 0;
// 遍历数组,将较小或相等的值复制到目标数组中。
// 此过程将在其中一个数组完全处理后停止。
for (; i < arr1.length && k < arr2.length; ) {
for (; k < arr2.length && i < arr1.length;) {
if (arr1[i] <= arr2[k]) {
result[r++] = arr1[i++];
} else {
result[r++] = arr2[k++];
}
}
}
在算法的这一步,其中一个数组必须已经完全处理。因此,尝试同时复制两个数组。对于空数组,while
循环基本上起到了条件判断的作用。
while (i < arr1.length) {
result[r++] = arr1[i++];
}
while (k < arr2.length) {
result[r++] = arr2[k++];
}
// 返回结果
return result;
}
英文:
The easiest way is to use a Stream.
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
- Stream both arrays.
- flatMap them to a single IntStream
- sort them
- convert to an array
int [] combined = Stream.of(arr1,arr2)
.flatMapToInt(Arrays::stream)
.sorted()
.toArray();
System.out.println(Arrays.toString(combined));
Prints
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
A non-stream approach can be done as follows:
// increase arr1 to make room for arr2
int oldLen = arr1.length;
arr1 = Arrays.copyOf(arr1, arr1.length+arr2.length);
// copy arr2 starting at 0, to arr1 starting at the old length
// positon of arr1 for a length of arr2
System.arraycopy(arr2, 0, arr1, oldLen, arr2.length);
// sort and print
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
Prints
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
Although your question, as asked, said nothing about merging
sorted arrays, here is how you would do it.
The algorithm is simple. Just iterate thru each array and compare current values.
- if
arr1[i] <= arr2[k]
, copyarr1[i] to result
, advancei
by1
- else copy
arr2[k] to result
, advancek
by1
. - in all cases the index to result,
r
, is advanced by1
public int[] merge(int[] arr1, int[] arr2) {
// result array
int[] result = new int[arr1.length + arr2.length];
int r = 0;
int k = 0;
int i = 0;
// Iterate thru the arrays, copying the lowest or equal value
// to the target array. This process will cease when one of the arrays
// has been fully processed.
for (; i < arr1.length && k < arr2.length; ) {
for (; k < arr2.length && i < arr1.length;) {
if (arr1[i] <= arr2[k]) {
result[r++] = arr1[i++];
}else {
result[r++] = arr2[k++];
}
}
}
Having reached this far in the algorithm, one of the arrays must have been completely processed. So try and copy both. For the empty array, the while loop
basically acts like an if statement.
while (i < arr1.length) {
result[r++] = arr1[i++];
}
while (k < arr2.length) {
result[r++] = arr2[k++];
}
// return the result
return result;
}
</details>
# 答案4
**得分**: 0
```java
public class Merger {
public static void main(String[] args) {
int[] arr1 = { 3, 3, 5, 6, 8, 9 };
int[] arr2 = { 3, 4, 5, 6 };
int[] res = merge(arr1, arr2);
System.out.println(Arrays.toString(res));
}
public static int[] merge(int[] arr1, int[] arr2) {
int[] res = new int[arr1.length + arr2.length];
for (int i = 0, a1 = 0, a2 = 0; i < res.length; i++) {
if (a1 == arr1.length)
res[i] = arr2[a2++];
else if (a2 == arr2.length)
res[i] = arr1[a1++];
else
res[i] = arr1[a1] <= arr2[a2] ? arr1[a1++] : arr2[a2++];
}
return res;
}
}
英文:
public class Merger {
public static void main(String[] args) {
int[] arr1 = { 3, 3, 5, 6, 8, 9 };
int[] arr2 = { 3, 4, 5, 6 };
int[] res = merge(arr1, arr2);
System.out.println(Arrays.toString(res));
}
public static int[] merge(int[] arr1, int[] arr2) {
int[] res = new int[arr1.length + arr2.length];
for (int i = 0, a1 = 0, a2 = 0; i < res.length; i++) {
if (a1 == arr1.length)
res[i] = arr2[a2++];
else if (a2 == arr2.length)
res[i] = arr1[a1++];
else
res[i] = arr1[a1] <= arr2[a2] ? arr1[a1++] : arr2[a2++];
}
return res;
}
}
答案5
得分: 0
一个简单的解决方案可以使用类Arrays
和System
编写。
步骤:
- 将
arr1[]
的元素复制到一个新数组中(称为output[]
),其大小为给定数组的大小之和。 - 在
arr1[]
的元素之后,将arr2[]
的元素复制到output[]
中。 - 对
output[]
进行排序。
演示:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = { 3, 3, 5, 6, 8, 9 };
int[] arr2 = { 3, 4, 5, 6 };
// 将arr1[]的元素复制到一个新数组中,其大小为给定数组的大小之和
int[] output = Arrays.copyOf(arr1, arr1.length + arr2.length);
// 在arr1[]的元素之后,将arr2[]的元素复制到output[]中
System.arraycopy(arr2.clone(), 0, output, arr1.length, arr2.length);
// 对output[]进行排序
Arrays.sort(output);
// 显示output[]
System.out.println(Arrays.toString(output));
}
}
输出:
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
英文:
A simple solution can be written using the classes, Arrays
and System
.
Steps:
- Copy elements of
arr1[]
into a new array (say,output[]
) whose size is the sum of the sizes of the given arrays. - Copy the elements of
arr2[]
, after the element ofarr1[]
, intooutput[]
- Sort
output[]
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = { 3, 3, 5, 6, 8, 9 };
int[] arr2 = { 3, 4, 5, 6 };
// Copy elements of arr1[] into a new array whose size is the sum of the sizes
// of the given arrays
int[] output = Arrays.copyOf(arr1, arr1.length + arr2.length);
// Copy the elements of arr2[], after the element of arr1[], into output[]
System.arraycopy(arr2.clone(), 0, output, arr1.length, arr2.length);
// Sort output[]
Arrays.sort(output);
// Display output[]
System.out.println(Arrays.toString(output));
}
}
Output:
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
答案6
得分: 0
你可以使用 System.arraycopy
方法来实现这个目的:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// 创建一个新数组,长度为两个数组长度之和
int[] arr3 = new int[arr1.length + arr2.length];
// 将第一个数组复制到总数组的开头
System.arraycopy(arr1, 0, arr3, 0, arr1.length);
// 将第二个数组复制到总数组的末尾
System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
// 对总数组进行排序
Arrays.sort(arr3);
System.out.println(Arrays.toString(arr3));
// [3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
英文:
You can use System.arraycopy
method for this purpose:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// create a new array of total length
int[] arr3 = new int[arr1.length + arr2.length];
// copy first array to the beginning of the total array
System.arraycopy(arr1, 0, arr3, 0, arr1.length);
// copy second array to the end of the total array
System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
// sort the total array
Arrays.sort(arr3);
System.out.println(Arrays.toString(arr3));
// [3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论