java中可以增长的二维对象

发布于 2024-11-28 17:23:48 字数 1304 浏览 1 评论 0原文

我需要将一个唯一的键与 Java 中的多个矩形对象中的每一个相关联。关键是double数据类型,而矩形显然是矩形数据类型。

目前,我在向量中拥有矩形,但它们对我来说没有多大用处,除非我也可以访问它们的键,如上面第一段中指定的那样。

我会创建一个二维数组,第一列是键,第二列是矩形,但是数组中的行数需要一直改变,所以我认为数组行不通。我研究过向量和数组列表,但我担心是否能够搜索和切片数据。

谁能告诉我一些简单的java代码来创建然后访问具有可变行数的二维数据集?

目前,我的原型如下所示:

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.");}
}

然后,我枚举数据以进行后续处理,如下所示:

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

如您所见,无法随后将键与矩形集成。这就是为什么我正在寻找一个 2D 对象来使用。谁能告诉我如何修复此代码,以便我可以将键与矩形相关联并随后访问适当关联的数据?

I need to associate a unique key with each of a number of rectangle objects in 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?

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. Can anyone show me how to fix this code so that I can associate keys with rectangles and subsequently access the appropriately associated data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蛮可爱 2024-12-05 17:23:48

为什么不简单地使用 HashMap

编辑:不,这存在重大问题,因为不能保证两个双精度数彼此相等,即使在数字上它们应该相等。一定要双份吗?可以使用其他数字或字符串表示形式,例如 Long 吗?您是否想要模拟一个物理现实?

Why not simply use a 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? Is there a physical reality that you're trying to model?

乱了心跳 2024-12-05 17:23:48

Why not use a 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()

丑疤怪 2024-12-05 17:23:48

Map 肯定是正确的答案,您不必担心映射函数的域或余域的基数。将 double 作为关键数据类型会禁止您使用某些预定义类型。

我会使用 TreeMap 只是因为自然排序用于对结构内的条目进行排序,因此完全允许使用双精度,但您会在检索时遇到问题(实际上,我使用自己的浮动作为地图的键,并且从未遇到过一些预防措施的问题,但这主要取决于数据的性质。

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. Having double as the key datatype forbids you from using some of the predefined types.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文