简体   繁体   English

解析数据结构中大小可变的数组-Java

[英]Parse varying sized arrays in data structure - Java

As a novice Java programmer, I've run into a rather high hurdle while trying to analyze data for a personal project of mine. 作为Java的新手程序员,在尝试分析我的个人项目的数据时遇到了很大的障碍。 I have a text file with ~33.5k data points in the following format: 我有一个文本文件,带有约33.5k数据点,格式如下:

PointNumber:        33530  
Lat:                8.99167773897820e-001  
Lon:                6.20173660875318e+000  
Alt:                0.00000000000000e+000  
NumberOfAccesses:   4  
0    4.80784667215499e+003   4.80872732950073e+003  
0    1.05264215520092e+004   1.05273043378212e+004  
1    1.65167780853593e+004   1.65185840063538e+004  
1    6.52228387069902e+004   6.52246514228552e+004  

The final rows, ie ones beginning with 0 or 1, correspond to the integer in the Number of Accesses row. 最后的行(即以0或1开头的行)对应于“访问次数”行中的整数。 I would like to parse through the file and print the Lat, Lon, and two values following each access instance of only PointNumbers having more than 1 number of accesses. 我想解析该文件,并在每个PointNumbers具有超过1次访问次数的每个访问实例之后打印Lat,Lon和两个值。

I'm not sure whether to start with a scanner or tokenizer or to compile a pattern. 我不确定是从扫描仪还是标记器开始还是要编译模式。 Which technique makes storing only valid PointNumbers easier? 哪种技术使仅存储有效的PointNumber变得容易? Intuition tells me to parse through the file, placing relevant values into an object: 直觉告诉我解析文件,将相关值放入对象中:

PointNumber num1 = new PointNumber(lat, lon, accesses[]);

Then loop through the objects and print the object's values if the accesses array length is > 1. On the other hand, it would be better to disregard the information that does not meet the requirements. 然后,如果访问数组的长度> 1,则遍历对象并打印对象的值。另一方面,最好忽略不符合要求的信息。 Could this be done by checking the NumberOfAccesses value while parsing then jump ahead to the next string.startsWith("PointNumber:") if the value is <= 1? 是否可以通过在解析时检查NumberOfAccesses值来完成,然后跳转到下一个string.startsWith(“ PointNumber:”)如果值<= 1?

I have a feeling the overwhelming majority of this community will try steering me towards XML or YAML, but I really prefer trying to tackle this in Java. 我有一种感觉,这个社区的绝大多数人会尝试将我引导到XML或YAML,但是我真的更喜欢尝试用Java解决这个问题。 Any advice, direction, or applicable examples is always greatly appreciated. 任何建议,指导或适用示例始终受到人们的赞赏。

You can loop through this while you have input to parse 您可以在输入解析后循环浏览

// Assuming you have a scanner object named s and pointed to your file:

// PointNumber: 33530
int pointNumber = 0;
while(!s.next().equals("PointNumber:")) {
    pointNumber = s.nextInt();
}

// Lat: 8.99167773897820e-001
double lat = 0.0;
while(!s.next().equals("Lat:")) {
    lat = s.nextDouble();
}

// Lon: 6.20173660875318e+000
double lon = 0.0;
while(!s.next().equals("Lon:")) {
    lon = s.nextDouble();
}

// Alt: 0.00000000000000e+000
double alt = 0.0;
while(!s.next().equals("Alt:")) {
    alt = s.nextDouble();
}

// NumberOfAccesses: 4
int numberOfAccesses = 0;
while(!s.next().equals("NumberOfAccesses:")) {
    numberOfAccesses = s.nextInt();
}

// 0 4.80784667215499e+003 4.80872732950073e+003
// 0 1.05264215520092e+004 1.05273043378212e+004
// 1 1.65167780853593e+004 1.65185840063538e+004
// 1 6.52228387069902e+004 6.52246514228552e+004

// Assuming you have defined an Access class
LinkedList<Access> accesses = new LinkedList<Access>();
for(int i = 0; i < numberOfAccesses; i++) {
    // Assuming this is the constructor for the Access class
    accesses.add(new Access(s.nextInt(), s.nextDouble(), s.nextDouble()));
}

Then you can add all the data you actually want into a list of "PointNumber" and then print or do whatever from it. 然后,您可以将所需的所有数据添加到“ PointNumber”列表中,然后打印或执行任何操作。

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

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