将文本文件中给出的两个数组合并为第三个数组

时间:2022-06-23 12:20:43

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 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++;
    }        
  }
}

2 个解决方案

#1


1  

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[]{});

#2


0  

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.

希望这会有所帮助,请再次询问更多详情,并随时发布您的进度。

#1


1  

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[]{});

#2


0  

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.

希望这会有所帮助,请再次询问更多详情,并随时发布您的进度。