简体   繁体   English

从列表字符串存储到另一个数组列表

[英]store from list string to another array list

I have a txt file like this which contains longitude and latitude coords: 我有一个这样的txt文件,其中包含经度和纬度坐标:

120.12    22.233
100    23.12
98     19.12

If I want to read the file, I am doing: 如果我想读取文件,我在做:

List<String> lines = Files.readAllLines(Paths.get(fileName));
System.out.println("LINE: " + lines.get(0));

and it gives me : 120 22 它给了我: 120 22

I have a class that reads latitude and longitude: 我有一堂课读纬度和经度:

public class GisPoints {

    private double lat;
    private double lon;

    public GisPoints() {
    }

    public GisPoints(double lat, double lon) {
       super();
       this.lat = lat;
       this.lon = lon;
    }

    //Getters and Setters
}

I want to store all the values from the txt file into an List<GisPoints> . 我想将txt文件中的所有值存储到List<GisPoints>

So, I want a function in order to load file: 所以,我想要一个函数来加载文件:

public static List<GisPoints> loadData(String fileName) throws IOException {

      List<String> lines = Files.readAllLines(Paths.get(fileName));

      List<GisPoints> points = new ArrayList<GisPoints>();

      //for (int i = 0; i < lines.size(); i++) {

     // }

      return points;

  }

As I said, right now I am just reading every line, for example lines[0] = 120 22 就像我说的,现在我正在读取每一行,例如lines[0] = 120 22

I want to store lines[0] longitude into points[0].setLon(), lines[0] latitude into points[0].setLat(). 我想将经线[0]存储到points [0] .setLon()中,将经线[0]纬度存储到points [0] .setLat()中。

This is an easy solution with String.split() 这是使用String.split()的简单解决方案

private GisPoints createGisPointsObjectFromLine(String p_line)
{
    String[] split = p_line.trim().split(" ");
    double lat = Double.parseDouble(split[0]);
    double lon = Double.parseDouble(split[split.length - 1]);
    return GisPoints(lat, lon);
}

You can call this method from your loadData() method. 您可以从loadData()方法调用此方法。 I hope this solution is helpful. 我希望此解决方案有帮助。 Good luck! 祝好运!

for (String str : lines) {
    String[] helpArray = str.split("\\s+"); // or whatever is betweeen
                                            // the two numbers
    points.add(new GisPoints(Double.valueOf(helpArray[0].trim()), Double.valueOf(helpArray[1].trim())));
}

this should work as you need it. 这应该在您需要时起作用。 only works as long as there are only numbers in the file. 仅在文件中只有数字的情况下才有效。 you can report back if you need further help 如果您需要进一步的帮助,可以报告

I want to store lines[0] longitude into points[0].setLon(), lines[0] latitude into points[0].setLat(). 我想将经线[0]存储到points [0] .setLon()中,将经线[0]纬度存储到points [0] .setLat()中。

You don't need setters right away. 您不需要马上设置。 You have a good constructor to receive both of them already. 您已经有一个很好的构造函数来接收它们两者。

Just create an object after splitting like into two parts. 将对象分成两部分后,只需创建一个对象即可。

 for (int i = 0; i < lines.size(); i++) { 
      String[] latlang = lines.get(i).split("\\s+");
      GisPoints g = new GisPoints(Double.parseDouble(latlang[0].trim()),Double.parseDouble(latlang[1].trim()));
      points.add(g)
    }

I think split can solve your problem: 我认为split可以解决您的问题:

String arr[] = lines[i].split("\\s+");
GisPoints p = new GisPoints(Double.parseDouble(arr[0]), Double.parseDouble(arr[1]));

You might want to do it with java8-streams: 您可能要使用java8-streams做到这一点:

 List<GisPoint> points = lines.stream()
        .map(s -> s.split("\\s+"))
        .map(array -> Arrays.stream(array)
            .map(String::trim)
            .filter(s -> !s.isEmpty())
            .mapToDouble(Double::parseDouble)
            .toArray()
        )
        .map(array -> new GisPoint(array[0], array[1]))
        .collect(Collectors.toList());

It would look like this: 它看起来像这样:

    for (int i = 0; i < lines.size(); i++)
    {
        String line = lines.get(i);

        String[] values = line.split("\\s+");

        double lat = Double.parseDouble(values[0]);
        double lon = Double.parseDouble(values[1]);

        points.add(new GisPoints(lat, lon));
    }

Take a look at the class Scanner; 看看班级Scanner; Scanner has a lot of useful tools for reading and parsing strings. 扫描仪有很多有用的工具来读取和解析字符串。

Take a look with regex : 用正则表达式看一下:

import java.util.regex.*;

public class HelloWorld{

     public static void main(String []args){
        String line = "98 19";
        Pattern pattern = Pattern.compile("^(\\d+)\\s*(\\d+)$");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            System.out.println("group 1: " + matcher.group(1));
            System.out.println("group 2: " + matcher.group(2));
        }
     }
}

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

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