0

我在一个文本文件中有 7 行数据(如下所示)。

name: abcd
temp: 623.2
vel: 8

name: xyz
temp: 432
vel: 7.6

使用正则表达式,我能够读取这些数据并且能够将其打印出来。现在我需要将这些数据存储在某个变量中。我倾向于将这些数据存储在数组/矩阵中。所以在物理上,它看起来像这样:

  data = [abcd, 623.2, 8 
          xyz, 432, 7.6]

所以实际上,第一行包含前 3 行,第二行包含从 5 到 7 的行。我选择这种类型的变量进行存储的原因是,从长远来看,调用数据会更简单——如:

   data[0][0] = abcd
   data[1][1] = 432 

我不能使用 math.nist.gov 中的 java 矩阵文件,因为我不是 root 用户,让 IT 部门在我的机器上安装东西被证明是浪费时间。所以我想使用我拥有的资源——Eclipse 和一个 java 安装版本 1.6。

我想获取这些数据并将其存储到 java 数组变量中。我想知道的是:选择数组变量是正确的方法吗?或者我应该使用向量变量(尽管如此,在我看来,使用向量变量会使事情复杂化)?还是有一些其他变量可以让我轻松存储数据并轻松调用它

顺便说一句,关于我的 java 安装的更多细节 - 以防它在某种程度上有所帮助:

OpenJDK Runtime Environment (build 1.6.0-b09) OpenJDK 64-bit Server VM (build 1.6.0-b09, 混合模式)

谢谢您的帮助

4

5 回答 5

9

It seems to me that

name: abcd
temp: 623.2
vel: 8

is some sort of object, and you'd do well to store a list of these e.g. you would define an object

public class MyObject {
   private String name;
   private double temp;
   private double vel;

   // etc...
}

(perhaps - there may be more appropriate types), and store these in a list:

List<MyObject>

If you need to index them via their name attribute, then perhaps store a map (e.g.Map<String, MyObject>) where the key is the name of the object.

I'm suggesting creating an object for these since it's trivially easy to ask for obj.getName() etc. rather than remember or calculate array index offsets. Going forwards, you'll be able to add behaviour to these objects (e.g. you have a temp field - with an object you can retrieve that in centigrade/kelvin/fahrenheit etc.). Storing the raw data in arrays doesn't really allow you to leverage the functionality of a OO language.

(note re your installation woes - these classes are native to the Java JRE/JDK and don't require installations. They're fundamental to many programs in Java)

于 2012-06-27T16:24:32.643 回答
3

您可以使用数组,但不要使用二维数组,而是创建一个包含元素的数据类,然后拥有这些元素的数组。

例如:

public class MyData {
  String name;
  float temp;
  int vel;
}

那么你可以定义

 MyData arr[];  

您也可以使用 List() 而不是 Array,具体取决于您是否有排序/搜索类型标准。如果您添加了一个元素,或者如果您想查找重复项或搜索,这种方法为您提供了更多的灵活性。

于 2012-06-27T16:25:32.890 回答
0

我认为你可以依赖java Collection 框架

  1. 如果数据中有特定的序列,您可以使用ArrayList 代替 Arrays
  2. 此外,如果您想将数据存储在键值对中,请使用Map

注意:如果您需要排序值,则将ArrayList 与 Comparator 或 Comparable Interface 一起使用。如果您正在使用Map 并且需要唯一且已排序的值,请使用 TreeMap

于 2012-06-27T16:30:00.197 回答
0

假设您正在读取的键值对中的所有键都是唯一的,为什么不将项目存储在java.util.Map?

Pattern pattern = Pattern.compile("(\\w+): (\\w+)");
try(BufferedReader reader = new BufferedReader(new FileReader("data.txt"))){

    Map<String, String> items = new LinkedHashMap<>();

    String line = null;
    while( (line = reader.readLine()) != null) {
        Matcher matcher = pattern.matcher(line);
        while(matcher.find()){
            items.put(matcher.group(1), matcher.group(2));
        }
    }

    System.out.println(items);

}catch(IOException e) {
    System.out.println(e.getMessage());
}

然后地图将包含:{name=xyz, temp=432, vel=7}

您可以轻松阅读特定元素,例如:items.get("name")

于 2012-06-27T16:36:45.190 回答
0

包装此信息

name: xyz
temp: 432
vel: 7.6

在它自己的一类中。

并使用List<T>您喜欢的任何实现。

于 2012-06-27T16:25:42.253 回答