[英]Compare elements of the same array
我在比較數組元素的值時遇到問題。 例如,我想比較索引 0 和索引 2 的值,以及索引 1 和索引 3 的值,依此類推。 使用下面的代碼,我想得到 numOfdifferentShape 的結果是 2,但我得到 3。我該如何解決這個問題? :-(
int numOfdifferentShape=0;
myArray = {40.0, 40.0, 40.0, 40.0, 80.0, 40.0, 40.0, 40.0}
for (int a=0; int a<myArray.size(); a=a+2)
{
for (int b=a+2; b<myArray.size; b=b+2)
{
if (!(myArray.get(a).equals(myArray.get(b) && myArray.get(a+1).equals(b+1)))
numOfdifferentShape++;
break;
}
}
這段代碼有幾個語法錯誤,但由於 TofuBeer 已經在評論中指出了它們,我將繼續進行設計和邏輯。
從代碼來看,我假設您對 Java 沒有太多經驗,也許根本沒有編程經驗。 所以我這里慢慢來go。 我希望你不會被我的解釋侮辱。
你說你試圖找出你在數組中存儲的對象(作為兩個整數)有多少是相等的。 為此,您必須跟蹤您已經看到的獨特對象。 然后,您將每個 object 與唯一對象列表進行比較,如果不匹配,則將其添加到列表中。 這是基本算法。
現在,你注意到我在描述中一直使用“對象”這個詞嗎? 發生這種情況時,通常意味着您應該制作 class。 我會做一個這樣的簡單的,持有兩個整數:
class Box { // or whatever the objects are called
private final int height;
private final int width;
public Box(int h, int w) {
height = h;
width = w;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Box))
return false;
Box b = (Box) other;
return b.height == height && b.width == width;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + this.height;
hash = 97 * hash + this.width;
return hash;
}
}
試着理解這段代碼的每一部分是做什么的(特別是如果這實際上是你的作業)。 一旦你得到它,繼續下一部分:做你想做的計算。
假設您有一個 Box 數組,如下所示:
Box[] boxes = {
new Box(40, 40), new Box(40, 40), new Box(80, 40), new Box(40, 40)
};
(我不知道你使用的是數組還是列表,所以我只是選擇一個來演示。)
我已經給出了查找唯一項目數量的算法,所以我將向您展示我將如何編寫它:
List<Box> unique = new ArrayList<Box>();
for (Box box : boxes) {
if (!unique.contains(box)) { // this is why I implemented equals() and hashCode()!
unique.add(box);
}
}
int numOfDifferentShape = unique.size();
這比嘗試為每個 object 跟蹤兩個整數要容易得多,而且它的優點是您不會混淆數組索引。
您可以使用Set
更輕松地做到這一點。 它看起來像這樣:
Set<Box> boxSet = new HashSet<Box>();
for (Box b : boxes)
boxSet.add(b);
int numOfDifferentShape = boxSet.size();
請注意,最后兩個片段使用Java 1.5中的功能,所以我不知道您之前是否遇到過它們。
這會讓事情變得更清楚嗎?
for (int i = 0; i < (myArray.size() - 2); ++i)
{
if (myArray[i] != myArray[i + 2])
++numOfdifferentShapes;
}
我認為你有一個括號問題。 你寫了:
if (!(myArray.get(a).equals(myArray.get(b) && myArray.get(a+1).equals(b+1)))
當我認為你的意思是:
if (!(myArray.get(a).equals(myArray.get(b)) && myArray.get(a+1).equals(b+1))
此外,在同一行,而不是:
equals(b+1)
你不是說
myArray.get(b+1)
我有數組列表,例如 {40,40,80,20,40,40} 我想比較元素。 偶數索引(例如索引 0、索引 2、索引 4 等)表示 object 的高度,奇數索引(例如索引 1、索引 3 ec)表示 object 的寬度。 因此,使用上面的代碼,Object 1(索引 0 和 1)。
為什么不創建一個維度 class 的數組,如下所示:
public class Dimension
{
private final int width;
private final int height;
public Dimension(final int w,
final int h)
{
width = w;
height = h;
}
public int getWidth()
{
return (width);
}
public int getHeight()
{
return (height);
}
}
然后做一個這樣的for循環:
for(int i = 0; i < array.length; i += 2)
{
final Dimension a;
final Dimension b;
a = array[i];
b = array[i + 1];
// compare a.getLength() to b.getLength()
// or
// compare a.getWidth() to b.getWidth()
}
嘗試變得“棘手”通常是一個壞主意 - 說偶數和奇數長度是棘手的......IMO壞主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.