views:

1422

answers:

5

I have problem with comparing the value of array elements. e.g. I wanted to compare the value of index 0 and index 2, and index 1 to index 3 and so on. With the code below I suppose to get the result of numOfdifferentShape is 2 but I get 3. How can I solve this problem? :-(

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;
   }
}
+2  A: 
for (int i = 0; i < (myArray.size() - 2); ++i)
{
    if (myArray[i] != myArray[i + 2])
        ++numOfdifferentShapes;
}
Ferdinand Beyer
I edited the code to reflect the fact that the original code compares pairs at a time. Had there been a sequence {40.0, 40.0, 80.0, 80.0}, the previous code would have returned 2 instead of 1.
Pesto
He said he supposed to get 2 so I think this is what he wants...?
Ferdinand Beyer
Thanks Ferdinand..I did some changes on the code based on your example and my code works now...for (int i = 0; i < (myArray.size() - 2); i = i + 2){ if (!(myArray.get(i).equals(myArray.get(i+2)) }
Jessy
+2  A: 
  1. You have two loops, your description suggests you only want one.
  2. You need to do bounds checking - do you want the n+2 to wrap to the start to the start of the array when it exceeds the length?
JeeBee
+2  A: 

I think you have a parentheses problem. You wrote:

if (!(myArray.get(a).equals(myArray.get(b) && myArray.get(a+1).equals(b+1)))

when I think you mean:

if (!(myArray.get(a).equals(myArray.get(b)) && myArray.get(a+1).equals(b+1))

Also, in the same line, instead of:

equals(b+1)

don't you mean

myArray.get(b+1)
JeffH
I think it's just a typo (like the invalid array definition) -- he reported his code runs but just gives a wrong result.
Ferdinand Beyer
his code cannot run... it isn't even close to valid Java. Either he us using a List or he is using an array. Arrays cannot be accessed the way he is doing it, and Lists cannot be created the way he is doing it.
TofuBeer
yes..myArray.get(b+1)...it was typo error. the code run but i didnt get the correct result.
Jessy
+3  A: 

There are several syntax errors in this code, but since TofuBeer has already pointed them out in the comments, I'll move on the the design and logic.

Going from the code, I'm assuming you don't have much experience with Java, and perhaps not with programming at all. So I'm going to go slowly here. I hope you aren't insulted by my explanations.

You say you are trying to find out how many of the objects which you are storing (as two ints) in your array are equal. To do this, you have to keep track of what unique objects you have already seen. Then you compare each object the list of unique objects and, if it doesn't match any of them, add it to the list. This is the basic algorithm.

Now, have you noticed that I keep using the word "object" in my description? When that happens, it usually means you should be making a class. I would make a simple one like this, holding the two integers:

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;
    }
}

Try to understand what each part of this code does (especially if this is actually your homework). Once you've got it, move on to the next part: doing the calculation that you were trying to do.

Let's say you have an array of Boxes, like this:

Box[] boxes = {
    new Box(40, 40), new Box(40, 40), new Box(80, 40), new Box(40, 40)
};

(I can't tell if you're using an array or a list, so I'm just picking one to demonstrate.)

I already gave the algorithm for finding the number of unique items, so I'll show you how I would write it:

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();

This is much easier than trying to keep track of two ints for each object, plus it has the advantage that you can't get your array indices confused.

You could do this even more easily with a Set. It would look something like this:

Set<Box> boxSet = new HashSet<Box>();
for (Box b : boxes)
    boxSet.add(b);
int numOfDifferentShape = boxSet.size();

Note that these last two snippets use features from Java 1.5, so I don't know if you've run into them before.

Does this make things clearer?

Michael Myers
lol... tag you're it (at least you got the longer answer this time!)
TofuBeer
Ha, I beat! (But I'll bet I started before you.)
Michael Myers
I am sure you did
TofuBeer
Thanks a lot for the example, I really appreciate it :-)
Jessy
+1  A: 

I have array list e.g. {40,40,80,20,40,40} I wanted to compare the elements. Even number of index (e.g. index 0, index 2, index 4 etc) represents Height of an object and Odd number of Index (e.g. index 1, index 3 ec) represent Width of an object. So, with the code above, Object 1 (index 0 and 1).

Why not make an array of a Dimension class, something like this:

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

then do a for loop something like this:

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() 
}

It is usually a bad idea to try and be "tricky" - saying even ones are with and odd ones are length is being tricky... bad idea IMO.

TofuBeer