简体   繁体   English

2D阵列排序

[英]2D arrays sorting

I have a text file, which was read into list and then converted to an array. 我有一个文本文件,该文件被读入列表,然后转换为数组。 In the text file every line is an element of a 2-dimensional array. 在文本文件中,每一行都是二维数组的元素。 However, the conversion from the text file to the array put the elements in an incorrect order, one that was not the same as the file. 但是,从文本文件到数组的转换使元素的顺序错误,这与文件顺序不同。

Here is my code so far: 到目前为止,这是我的代码:

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
* @(#)ReadFromFile.java
*
*
* @author 
* @version 1.00 2016/2/25
*/

public class ReadFromFile {

  public static String[] readFile(String path){
    try{
      int i = 0;
      int length = Files.readAllLines(Paths.get(path)).size();
      String[] results = new String[length];
      for(String line : Files.readAllLines(Paths.get(path))){
        results[i++] = line;
      }
      return results;
    } catch(IOException e) {
      e.printStackTrace();
      return null;
    }
  }

  public static void main(String[] args) {
    // Operation for Prescription File
    String[] lines = readFile("prescription.txt");
    String[] array = new String[7];
    ArrayList<String> mylist = new ArrayList<String>();
    for(String line : lines){
        for (String s : line.split("    ")){
        mylist.add(s);
        }
    }
    String[] pre = mylist.toArray(array);// Stores the elements in     prescription txt
    System.out.println(Arrays.toString(pre));
    // Operation for medicament file`enter code here`
    String[] lines2 = readFile("medicaments.txt");
    ArrayList<String> mylist2 =`enter code here` new ArrayList<String>();
    int count = 0;// varıable for countıng lines of medicaments file
    for(String line2 : lines2){
        count++;
        for (String s : line2.split("   ")){
        mylist2.add(s);
        }
   }
    String[] array2 = new String[count * 5];
    String[] med = mylist2.toArray(array2);// Stores the elements in   prescription txt
    //Converting (medicaments.txt) 1d array into 2d array
   String array2d[][] = new String[count][5];

    for(int i=0; i<count;i++){
       for(int j=0;j<5;j++){
          array2d[i][j] = med[(j*count) + i];
       }
    }
    System.out.println(Arrays.deepToString(array2d));
    // Initialize array variables to compare
    String medicine1 = pre[3];
    String medicine2 = pre[5];
    String numOfmedicine1 = pre[4];
    String numOfmedicine2 = pre[6];
    String socSecurity = pre[1];
    String date = pre[1];
  }
}

And here is my text document: 这是我的文字文件:

aspirin ssk 01.01.2016  31.01.2016  5.5
aspirin ssk 01.01.2016  31.01.2016  4
aspirin es  01.01.2016  31.01.2016  5.4
aspirin bk  01.01.2016  31.01.2016  6.1
aspirin ssk 01.01.2016  31.01.2016  5.4
novalgin    ssk 01.01.2016  31.01.2016  8.3

The output elements within each line come out in different order: 每行中的输出元素以不同的顺序出现:

[[aspirin, ssk, 01.01.2016, 31.01.2016, 5.4], [ssk, 01.01.2016, 31.01.2016, 6.1, novalgin], [01.01.2016, 31.01.2016, 5.4, aspirin, ssk], [31.01.2016, 4, aspirin, ssk, 01.01.2016], [5.5, aspirin, bk, 01.01.2016, 31.01.2016], [aspirin, es, 01.01.2016, 31.01.2016, 8.3]]

How do I correct this? 我该如何纠正?

First, you're overcomplicating a lot. 首先,您过于繁琐。 Your entire readFile function unnecessarily converts a List<String> to an array of strings, only for you to convert it back. 您的整个readFile函数不必要地将List<String>转换为List<String>数组,仅用于将其转换回来。 Your class should use Files.readAllLines() directly, avoid using arrays directly in favor of full list options and look something like this: 您的类应直接使用Files.readAllLines() ,避免直接使用数组来支持完整列表选项,并且应如下所示:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.io.IOException;

public class ReadFromFile {

  public static List<String> getWords(String path) {
    try {
      List<String> lines = Files.readAllLines(Paths.get(path));
      List<String> words = new ArrayList<>();  //Use descriptive variable names!
      for (String line : lines) {
        for (String word : line.split("    ")) {
          words.add(word);
          System.out.println(word);
        }
      }
      return words;
    } catch (IOException e) {
      //handle exception
      return null;  //This is not advised
    }
  }

  public static void main(String[] args) {
    List<String> prescription_words = getWords("prescription.txt");
    List<String> medicaments_words = getWords("medicaments.txt");

    List<List<String>> list2d = new ArrayList();

    for (int idx=0; idx < medicaments_words.size(); idx++) {
      // Iterate through your medications and add them to your 2dArray
    }
  }
}

Now, this doesn't address two things. 现在,这不能解决两件事。 First, your original code has two text files, only one of which is specified (which?). 首先,您的原始代码有两个文本文件,仅指定了其中一个(哪个?)。 Secondly, you are unclear how the conversion should actually occur: 其次,您不清楚转换应如何实际发生:

for(int i=0; i<count;i++){
   for(int j=0;j<5;j++){
      array2d[i][j] = med[(j*count) + i];
   }
}

You are unclear (mostly because count , i and j are bad variables to use) where you want what to end up. 您不清楚(主要是因为countij是要使用的错误变量)在什么地方想要结束。 This is much more readable: 这更具可读性:

for(int row=0; row < number_of_meds; row++){
   for(int column=0; column < 5; column++){
      index_of_medication = (column * number_of_meds) + row
      array2d[row][column] = med[index_of_medication];
   }
}

But this is sort of a weird way to phrase it. 但这是一种奇怪的说法。 What you really want to do is iterate through your medications and place them in the 2d array. 您真正想要做的是遍历您的药物并将其放置在2d阵列中。 All you have to do is set up the outer array first, then add elements to each inner array. 您要做的就是首先设置外部数组,然后向每个内部数组添加元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM