[英]Merging Two Arrays Given In a Text File into a Third Array
Yes this is homework. 是的,这是家庭作业。 Simply put because my professor isn't the best at wording,
简而言之,因为我的教授的措辞不是最好的,
I am given a text file as such 我得到了这样的文本文件
11 -5 -4 -3 -2 -1 6 7 8 9 10 11
8 -33 -22 -11 44 55 66 77 88
This is given as SortedArrays.txt 给出为SortedArrays.txt
The first row would be Array 1 and the second row would be Array 2, the first int of each line would be the size of array so: 第一行将是数组1,第二行将是数组2,每行的第一int将是数组的大小,因此:
int a[] = new int[11]
int b[] = new int[8]
My first issue is reading this into the program, I'm assuming run as an argument, but my issue is deciphering between each line and loading the array. 我的第一个问题是将其读入程序,我假设将其作为参数运行,但是我的问题是在每一行之间解密并加载数组。
I believe I have a good method for comparing the a and b elements and loading into c, but can anyone take a look over it? 我相信我有一个比较a和b元素并将其加载到c中的好方法,但是有人可以看一下吗?
Thanks again for this site, I've searched around and have seen this program but the elements were already given or not given from a text file. 再次感谢您访问本网站,我到处搜索并看到了该程序,但是元素已经从文本文件中给出或未给出。
// ************************************************************
// MergeArray.java
//
// Written by: Brandon Pham
//
//
//
//
// Homework 6
// ************************************************************
import java.io.*;
import java.util.*;
public class MergeArray {
String arrayfile = "SortedArrays.txt";
MergeArray(String arrayfile){
try {
Scanner arrayload = new Scanner(File(arrayfile));
}
catch (FileNotFoundException e) {
System.out.printf("File %s not found, exiting!",
arrayfile);
System.exit(0);
}
int[] a = new int[arrayload.nextint()];
int[] b = new int[arrayload.nextLine()];
int[] c = new int[a.length+b.length];
}
public static void merge(int[]a, int[]b, int[]c) {
int i=0,j=0,k=0;
int alength = a.length;
int blength = b.length;
while (k<c.length){
if (a[i] < b[j]){
c[k] = a[i];
k++;
i++;
}
if (a[i] == b[j]){
c[k]=a[i];
c[k++]=b[j];
i++;
j++;
if (i+1>a.length){
c[k]=b[j];
k++;
j++;
}
if (j+1>b.length){
c[k]=a[i];
k++;
i++;
}
else{
c[k] = b[j];
k++;
j++;
}
}
}
Try this : read one line at a time, split the line using space to get array of numbers in the line, add all entries to a list as it has open end. 尝试以下操作:一次读取一行,使用空格分割该行以获取该行中的数字数组,并将所有条目添加到列表中,因为它具有开放式结尾。 Finally get the array from the list.
最后从列表中获取数组。
String line;
List<String> list = new ArrayList<String>();
while(arrayload.hasNextLine()){
line = arrayload.nextLine();
String[] nums = line.split(" ");
list.addAll(Arrays.asList(nums));
}
String[] finalNums = list.toArray(new String[]{});
Your file reading's good. 您的文件阅读很好。 The sorting of the output however, could be a bit simpler:
但是,输出的排序可能会更简单:
Here's some quick code: 这是一些快速代码:
int[] result = new int[array1.length+array2.length]
int cursor1 = 0;
int cursor2 = 0;
for(int = 0; i < result.length; i++) {
if(cursor1 > array1.length-1){
result[i] = array2[cursor2++];
} else if(cursor2 > array2.length-1){
result[i] = array1[cursor1++];
} else if(array1[cursor1] > array2[cursor2]){
result[i] = array1[cursor1];
cursor1++;
} else {
result[i] = array2[cursor2];
cursor2++;
}
}
I just coded this in the browser, so I'm certainly not vouching for correct syntax ;) 我只是在浏览器中编写了代码,所以我当然不保证使用正确的语法;)
Hope this helps, please ask again for further details, and feel free to post your progress. 希望对您有所帮助,请再次询问更多详细信息,并随时发布您的进度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.