简体   繁体   English

将一个班级的信息添加到另一个班级的数组列表中

[英]Adding one class' info to an array list in another class

I'm making a project in Java that's a basic GPS process. 我正在用Java做一个基本的GPS流程项目。 It has a point class that is the current location, shown below; 它的点类是当前位置,如下所示;

public class Point {
// Constants useful for bounds checking, etc

private static final double MIN_LONGITUDE = -180.0;
private static final double MAX_LONGITUDE = 180.0;
private static final double MIN_LATITUDE = -90.0;
private static final double MAX_LATITUDE = 90.0;
private static final double MEAN_EARTH_RADIUS = 6.371009e+6;

// TODO: Define fields for time, longitude, latitude and elevation

public static ZonedDateTime time = ZonedDateTime.now();
public static double longitude;
public static double latitude;
public static double elevation;

// TODO: Define a constructor

public Point(ZonedDateTime timestamp, double longitude, double latitude, double elevation) {


    this.longitude = longitude;
    this.latitude = latitude;
    this.elevation = elevation;

}

Point point1 = new Point ( time, -1.54853, 53.80462, 72.5);

// TODO: Define getters for the fields


public static ZonedDateTime getTime() {
    return time;
}

public static double getLongitude() {
    return longitude;
}

public static double getLatitude() {
    return latitude;
}

public static double getElevation() {
    return elevation;
}

// TODO: Define a toString() method that meets requirements
public String toString() {

    return "( " + longitude + ", " + latitude + " ),  " + elevation;
}

Then another class, track, that is supposed to be a collection of points showing the journey. 然后是另一个类,跟踪,它应该是显示旅程的点的集合。 However, I am struggling with how to go about this, I have created a linked list but I'm unsure where to go from here. 但是,我正在为如何解决这个问题而苦苦挣扎,我创建了一个链表,但不确定从何处去。 I've been messing around with extending the class but having identifier expected errors when using Point in the add method. 我一直在扩展类,但是在add方法中使用Point时出现标识符预期错误。

import java.time.ZonedDateTime; 导入java.time.ZonedDateTime; import java.util.ArrayList; 导入java.util.ArrayList;

class Track{ 课程跟踪{

//public LinkedList<Point> Track = new LinkedList<>();


public Track() {

    ArrayList<Point> Track = new ArrayList<>();

}

public static void add(Object Point){

    Track.add((Point));
}

} But I am having trouble referencing the new array created by track() to add the next point. 但是,我在引用track()创建的新数组以添加下一点时遇到了麻烦。

Ok so you are trying to do a number of different things here - the way I am going to place it is not the only way, but something to consider...I have put everything into a package so you will need to restructure in your eclipse setup accordingly... 好的,所以您尝试在这里做很多不同的事情-我要放置的方法不是唯一的方法,而是要考虑的事情...我已经将所有内容都放入了一个包中,因此您需要在自己的文件中进行重组蚀设置相应...

Made some modification here because you are referring vars and methods in a static way, not that you can't do that, but for the purpose of what you are trying to achieve this is probably a better learning experience. 在此处进行一些修改是因为您以静态方式引用var和方法,不是您不能做到这一点,但是出于您想要实现的目的,这可能是一种更好的学习体验。 I also removed code that was dead...The timestamp portion may not be what you are looking for - but it seemed like a better fit here. 我还删除了已死的代码...时间戳部分可能不是您想要的-但似乎更适合此处。

package com.gps;
import java.time.ZonedDateTime;

public class Point {

    // TODO: Define fields for time, longitude, latitude and elevation

    public ZonedDateTime time;
    public double longitude;
    public double latitude;
    public double elevation;

    public Point(ZonedDateTime timeStamp, double longitude, double latitude, double elevation) {
        this.time = timeStamp;
        this.longitude = longitude;
        this.latitude = latitude;
        this.elevation = elevation;

    }

    // TODO: Define getters for the fields

    public ZonedDateTime getTime() {
        return time;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getElevation() {
        return elevation;
    }

    @Override
    public String toString() {
        return "Point [time=" + time + ", longitude=" + longitude + ", latitude=" + latitude + ", elevation="
                + elevation + "]";
    }



}

Here I added a toString() so you can see it when you print from the Engine class, I have renamed your variables and methods as I mentioned in my first post so you can keep track of whats what - it will matter make sure you do it... 在这里,我添加了一个toString(),以便从Engine类进行打印时可以看到它,正如我在第一篇文章中提到的那样,我已重命名了变量和方法,以便可以跟踪最新情况-确保执行此操作很重要它...

package com.gps;

import java.util.ArrayList;

public class Track {

     ArrayList<Point> track = new ArrayList<>();


    public void add(Point point){

        track.add(point);
    }


    @Override
    public String toString() {
        return "Track [track=" + track + "]";
    }


}

Since the only data I have to work with is the one that you supplied that is what I am returning - the date might not be right, but it worked well from Mykong's examples :) 由于我要处理的唯一数据是您提供的返回的数据-日期可能不正确,但从Mykong的示例中来看,它工作得很好:)

package com.gps;

import java.time.ZonedDateTime;

public class Engine {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Track track = new Track();
        Point point = new Point (ZonedDateTime.now(), -1.54853, 53.80462, 72.5);
        System.out.println(point.getTime());
        track.add(point);

        System.out.println(track);
    }

}

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

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