简体   繁体   English

可以在 java 中生长的二维 object

[英]2-dimensional object that can grow in java

I need to associate a unique key with each of a number of rectangle objects in Java.我需要将唯一键与 Java 中的多个矩形对象中的每一个相关联。 The key is in double data type, and the rectangles are obviously rectangle data types.关键是双精度数据类型,而矩形显然是矩形数据类型。

Currently, I have the rectangles in a vector, but they are not of much use to me unless I can also access their keys, as specified in the first paragraph above.目前,我在向量中有矩形,但它们对我没有多大用处,除非我也可以访问它们的键,如上面第一段中所述。

I would make a 2d array, with the first column being the key and the second column being the rectangle, but the number of rows in the array will need to change all the time, so I do not think an array would work.我会制作一个二维数组,第一列是键,第二列是矩形,但是数组中的行数需要一直改变,所以我不认为数组会起作用。 I have looked into vectors and arrayLists, but I am concerned about being able to search and slice the data.我研究了向量和数组列表,但我担心能够搜索和切片数据。

Can anyone show me some simple java code for creating and then accessing a 2D data set with a variable number of rows?谁能告诉我一些简单的 java 代码,用于创建然后访问具有可变行数的二维数据集?

Currently, my prototype looks like:目前,我的原型看起来像:

ArrayList<Double> PeakList = new ArrayList<Double>();
Vector<Rectangle> peakVector = new Vector<Rectangle>();
Vector<Double> keyVector = new Vector<Double>();

if(PeakList.contains((double)i+newStartingPoint)){
    Rectangle myRect = new Rectangle(x2-5, y2-5, 10, 10);
    boolean rectFound = peakVector.contains(myRect);
    System.out.println("rectFound is:  "+rectFound);
              Double myPeak = ((double)i+newStartingPoint);
    if(rectFound!=true){
        peakVector.add(myRect);
                       keyVector.add(myPeak);
        System.out.println("rectFound was added.");
    }else{System.out.println("rectFound was NOT added.");}
}

I then enumerate through the data for subsequent processing with something like the following:然后我枚举数据以进行后续处理,如下所示:

Enumeration e = peakVector.elements();
    while (e.hasMoreElements()) {
        g2.fillRect(peakVector.lastElement().x, peakVector.lastElement().y, 10, 10);
    }

As you can see, there is no way to subsequently integrate the keys with the rectangles.如您所见,没有办法随后将键与矩形集成。 That is why I am looking for a 2D object to use.这就是为什么我正在寻找一个 2D object 来使用。 Can anyone show me how to fix this code so that I can associate keys with rectangles and subsequently access the appropriately associated data?谁能告诉我如何修复此代码,以便我可以将键与矩形关联并随后访问适当关联的数据?

Why not simply use a HashMap<Double, Rectangle> ?为什么不简单地使用HashMap<Double, Rectangle>

Edit: no, there are significant problems with this since there's no guarantee that two doubles will equal each other even though numerically they should.编辑:不,这有很大的问题,因为不能保证两个双打将彼此相等,即使它们在数字上应该相等。 Does it have to be Double?一定要双倍吗? Could use use some other numeric or String representation such as a Long?可以使用其他一些数字或字符串表示,例如 Long 吗? Is there a physical reality that you're trying to model?您是否正在尝试 model 的物理现实?

Why not use a Map ?为什么不使用Map They are specifically designed to associate keys with values.它们专门设计用于将键与值相关联。 You can iterate through the keys of the map with keySet() , the values with valueSet() and both the keys and values at the same time with entrySet()您可以使用keySet()遍历 map 的键,使用valueSet()遍历值,使用entrySet()同时遍历键和值

A Map will surely be the right answer, you don't worry about cardinality of the domain or of the codomain of the mapping function. Map肯定是正确答案,您不必担心映射 function 的域或共域的基数。 Having double as the key datatype forbids you from using some of the predefined types.double作为关键数据类型会禁止您使用某些预定义类型。

I would go with a TreeMap<Double, Rectangle> just because the natural ordering is used to sort the entries inside the structure, so having a double is perfectly allowed, but you would have problems with the retrieval (I actually used myself floats as keys for maps and never had a problem with some precautions but it mostly depends on the nature of your data.我将 go 与TreeMap<Double, Rectangle>只是因为自然排序用于对结构内的条目进行排序,所以完全允许使用双精度,但检索时会遇到问题(我实际上使用自己的浮点数作为键对于地图,并且在某些预防措施方面从未遇到过问题,但这主要取决于您数据的性质。

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

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