Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二维数组排序_Java_Arrays_Sorting - Fatal编程技术网

Java 二维数组排序

Java 二维数组排序,java,arrays,sorting,Java,Arrays,Sorting,我有一个文本文件,它被读入列表,然后转换成数组。在文本文件中,每一行都是二维数组的一个元素。但是,从文本文件到数组的转换使元素的顺序不正确,与文件的顺序不同 以下是我目前的代码: 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.IOE

我有一个文本文件,它被读入列表,然后转换成数组。在文本文件中,每一行都是二维数组的一个元素。但是,从文本文件到数组的转换使元素的顺序不正确,与文件的顺序不同

以下是我目前的代码:

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];
  }
}
每行中的输出元素按不同顺序排列:

[[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]]

如何更正此问题?

首先,您的操作过于复杂。您的整个
readFile
函数不必要地将
列表
转换为字符串数组,只需要您将其转换回。您的类应直接使用,避免直接使用数组,而应使用完整列表选项,如下所示:

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

但这是一种奇怪的表达方式。你真正想做的是迭代你的药物并将它们放置在2d数组中。您所要做的就是先设置外部数组,然后向每个内部数组添加元素。

首先,您的操作过于复杂。您的整个
readFile
函数不必要地将
列表
转换为字符串数组,只需要您将其转换回。您的类应直接使用,避免直接使用数组,而应使用完整列表选项,如下所示:

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

但这是一种奇怪的表达方式。你真正想做的是迭代你的药物并将它们放置在2d数组中。您只需先设置外部数组,然后将元素添加到每个内部数组。

文本文档没有按我所希望的方式打印出来。请尝试简化代码,使其可读。请注意,您在该代码中导入了两个文本文件。你能提供一个完整的输入示例吗?文本文档并没有按我所希望的那样打印出来。这里是:请尝试简化代码,使其可读。注意,你在这段代码中导入了两个文本文件。您能提供完整的输入样本吗?@HabilGanbarli请务必对帮助您的答案进行投票!此外,通过“接受”答案,您可以在将来邀请更多的帮助。@HabilGanbarli请务必对帮助您的答案进行投票!此外,通过“接受”答案,你可以在未来邀请更多的帮助。
for(int i=0; i<count;i++){
   for(int j=0;j<5;j++){
      array2d[i][j] = med[(j*count) + i];
   }
}
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];
   }
}