Java 是否在自己的类中存储数组数组?
我写了一个程序,其中有4个数字。 第一个数字是一个ID(int) 其他3个只是地图上的点。(双份)Java 是否在自己的类中存储数组数组?,java,arrays,class,data-structures,Java,Arrays,Class,Data Structures,我写了一个程序,其中有4个数字。 第一个数字是一个ID(int) 其他3个只是地图上的点。(双份) 我想在它自己的类中创建一个数组,理想情况下,它存储这3个数字,并使用第一个作为ID arraylist是一种好方法吗? 我真的只是在数据结构上挣扎。它的本质很简单。我只需要在数组中存储一组数字。我能做到。然而,将其存储在不同的类中是困难的 无需创建新的类4 Number[][] lst = {{1, 3.0, 4.0, 11.0, 443.f}}; 或者像这样: HashSet<Integ
我想在它自己的类中创建一个数组,理想情况下,它存储这3个数字,并使用第一个作为ID arraylist是一种好方法吗?
我真的只是在数据结构上挣扎。它的本质很简单。我只需要在数组中存储一组数字。我能做到。然而,将其存储在不同的类中是困难的 无需创建新的类4
Number[][] lst = {{1, 3.0, 4.0, 11.0, 443.f}};
或者像这样:
HashSet<Integer, <ArrayList<Double>> items = new HashSet<Integer, <ArrayList<Double>>();
public class MapPoints {
private int id;
private double point1;
private double point2;
private double point3;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setPoint1(double point1) {
this.point1 = point1;
}
public double getPoint1() {
return point1;
}
public void setPoint2(double point2) {
this.point2 = point2;
}
public double getPoint2() {
return point2;
}
public void setPoint3(double point3) {
this.point3 = point3;
}
public double getPoint3() {
return point3;
}
}
HashSet无需创建新的类4
Number[][] lst = {{1, 3.0, 4.0, 11.0, 443.f}};
或者像这样:
HashSet<Integer, <ArrayList<Double>> items = new HashSet<Integer, <ArrayList<Double>>();
public class MapPoints {
private int id;
private double point1;
private double point2;
private double point3;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setPoint1(double point1) {
this.point1 = point1;
}
public double getPoint1() {
return point1;
}
public void setPoint2(double point2) {
this.point2 = point2;
}
public double getPoint2() {
return point2;
}
public void setPoint3(double point3) {
this.point3 = point3;
}
public double getPoint3() {
return point3;
}
}
HashSet因为Java是一种面向对象的语言,所以只需使用自定义类:
class MyPoint {
double x, y, z;
}
或者如果你真的需要一个数组
class MyPoint {
double[] coords;
}
然后它取决于你的id是什么,如果它是一个没有“洞”的自动递增值,如果你需要随机访问,你只需要一个ArrayList
如果它是一个稀疏索引,您仍然需要随机访问,那么使用HashMap
。如果需要对它们进行排序(按id或按位置),可以使用树映射。因为Java是一种面向对象的语言,所以只需使用自定义类:
class MyPoint {
double x, y, z;
}
或者如果你真的需要一个数组
class MyPoint {
double[] coords;
}
然后它取决于你的id是什么,如果它是一个没有“洞”的自动递增值,如果你需要随机访问,你只需要一个ArrayList
如果它是一个稀疏索引,您仍然需要随机访问,那么使用HashMap
。如果你需要对它们进行排序(如果是ID或位置),则可以使用“<代码> TeMeMAP <代码>。 < P>如果这三个点相互关联,请考虑为它们引入一种类型,如“代码> MyPosits <代码>,并将它们存储在这样的查找表中:
HashSet<Integer, <ArrayList<Double>> items = new HashSet<Integer, <ArrayList<Double>>();
public class MapPoints {
private int id;
private double point1;
private double point2;
private double point3;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setPoint1(double point1) {
this.point1 = point1;
}
public double getPoint1() {
return point1;
}
public void setPoint2(double point2) {
this.point2 = point2;
}
public double getPoint2() {
return point2;
}
public void setPoint3(double point3) {
this.point3 = point3;
}
public double getPoint3() {
return point3;
}
}
Map myMap=newhashmap
<>记住重写<代码> hash码< /COD>和<代码>等于<代码> MyPosits <代码>,以使MAP按预期的功能运行,即考虑两个代码> MyPosie<代码>相同内容相同的实例。 < P>如果这三个点彼此相关,请考虑为它们引入一个类型,如MyPoints
并将其存储在地图中,以便进行如下查找:
HashSet<Integer, <ArrayList<Double>> items = new HashSet<Integer, <ArrayList<Double>>();
public class MapPoints {
private int id;
private double point1;
private double point2;
private double point3;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setPoint1(double point1) {
this.point1 = point1;
}
public double getPoint1() {
return point1;
}
public void setPoint2(double point2) {
this.point2 = point2;
}
public double getPoint2() {
return point2;
}
public void setPoint3(double point3) {
this.point3 = point3;
}
public double getPoint3() {
return point3;
}
}
Map myMap=newhashmap
请记住覆盖hashCode
和equals
方法,以使映射按预期运行,即考虑两个内容相同的MyPoint
实例为相等。您也可以使用map
并使用id作为键,然后使用包含点的数组
Map<Integer,Double[]> map = new HashMap<Integer,Double[]>();
Map Map=newhashmap();
您还可以使用地图
并将您的id用作键,然后使用包含点的数组
Map<Integer,Double[]> map = new HashMap<Integer,Double[]>();
Map Map=newhashmap();
我的解决方案是这样的
class MyPoint {
int id;
double[] coordinate= new double[3];
MyPoint(int id, double x,double y, double z) {
this.id = id;
coordinate[0]= x;
coordinate[1]= y;
coordinate[2]= z;
}
public int getId() {
return id;
}
public double[] getCoordinate() {
return coordinate;
}
}
class MyPoints extends ArrayList<MyPoint> {
public void addPoint(int id, double x,double y, double z) {
super.add(new MyPoint(id,x,y,z));
}
}
类MyPoint{
int-id;
double[]坐标=新的double[3];
MyPoint(整数id,双x,双y,双z){
this.id=id;
坐标[0]=x;
坐标[1]=y;
坐标[2]=z;
}
公共int getId(){
返回id;
}
公共双[]getCoordinate(){
返回坐标;
}
}
类MyPoints扩展了ArrayList{
公共无效添加点(整数id,双x,双y,双z){
super.add(新的MyPoint(id,x,y,z));
}
}
注:
如果需要找到id为的MyPoint,那么可以使用HashMap
class MyPointWithHashMap extends HashMap<Integer,MyPoint> {
public void addPoint(int id, double x,double y, double z) {
super.put(id,new MyPoint(id,x,y,z));
}
}
class MyPointWithHashMap扩展了HashMap{
公共无效添加点(整数id,双x,双y,双z){
super.put(id,新的MyPoint(id,x,y,z));
}
}
我的解决方案是这样的
class MyPoint {
int id;
double[] coordinate= new double[3];
MyPoint(int id, double x,double y, double z) {
this.id = id;
coordinate[0]= x;
coordinate[1]= y;
coordinate[2]= z;
}
public int getId() {
return id;
}
public double[] getCoordinate() {
return coordinate;
}
}
class MyPoints extends ArrayList<MyPoint> {
public void addPoint(int id, double x,double y, double z) {
super.add(new MyPoint(id,x,y,z));
}
}
类MyPoint{
int-id;
double[]坐标=新的double[3];
MyPoint(整数id,双x,双y,双z){
this.id=id;
坐标[0]=x;
坐标[1]=y;
坐标[2]=z;
}
公共int getId(){
返回id;
}
公共双[]getCoordinate(){
返回坐标;
}
}
类MyPoints扩展了ArrayList{
公共无效添加点(整数id,双x,双y,双z){
super.add(新的MyPoint(id,x,y,z));
}
}
注:
如果需要找到id为的MyPoint,那么可以使用HashMap
class MyPointWithHashMap extends HashMap<Integer,MyPoint> {
public void addPoint(int id, double x,double y, double z) {
super.put(id,new MyPoint(id,x,y,z));
}
}
class MyPointWithHashMap扩展了HashMap{
公共无效添加点(整数id,双x,双y,双z){
super.put(id,新的MyPoint(id,x,y,z));
}
}
我可能会为三点坐标系创建一个包装类,但不会为包括ID在内的整个对象创建包装类。ID可能应该是单独的,因为它是坐标本身之上的一个抽象级别
public class Point {
double x, y, z;
//standard constructors, getters, setters
}
然后,无论你在哪里使用这个,你都可以
HashMap<Integer, Point> map = new HashMap<Integer, Point>();
int id = 0;
这会为每个坐标集提供一个唯一的ID,同时不会使ID成为对象的固有部分。我可能会为三点坐标系创建一个包装类,但不会为包括ID在内的整个对象创建包装类。ID可能应该是独立的,因为它是坐标本身之上的一个抽象级别
public class Point {
double x, y, z;
//standard constructors, getters, setters
}
然后,无论你在哪里使用这个,你都可以
HashMap<Integer, Point> map = new HashMap<Integer, Point>();
int id = 0;
这将为每个坐标集提供一个唯一的ID,同时不会使ID成为对象的固有部分。为什么不创建一个模型类并在控制器类中使用它?我想那会让我容易得多
class SomeModel {
// getters and setters }
class Controller {
public SomeModel model;
//getter and setter for model
}
为什么不创建一个模型类并在控制器类中使用它呢?我想那会让我容易得多
class SomeModel {
// getters and setters }
class Controller {
public SomeModel model;
//getter and setter for model
}
为什么不使用Apache Commons集合中的MultiMap
:
MultiMap
为同一个键关联多个值。(这是透明的,这就是为什么在这种情况下,MultiMap
非常有用的原因)
然后,您可以检索特定id的双重列表:
List list = (List) idWithDoubles.get(1);
默认情况下返回值为1.1、1.2、1.3的ArrayList
您还可以在此处看到番石榴的版本(最新版本):
为什么不使用Apache Commons Collections中的多地图
:
MultiMap
ass