Solved

Core java. Please help me catch a bug.

Posted on 2016-08-22
5
81 Views
Last Modified: 2016-08-31
To make story short, I simplify the code as much as possible.  Please help me to catch a bug. What do I miss ? I know about HashSet and I know how to use google. I'm interesting in my particular code.

package TaskChAll;

import java.util.Arrays;


public class Taskv4_interestingBUG2 {

    public static String removeAllDuplications(int[] initialArray) {
        int size = initialArray.length;
        System.out.println("Size before deletion: " + size);
        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (initialArray[i] == initialArray[j]) {   // step 1
                    while (j < (size) - 1) {    // inside this loop step 2
                        initialArray[j] = initialArray[j + 1];
                        j++;
                    }
                    size--; // step 3
                }
            }
        }
        int[] outputArray = new int[size]; // step 4
        for (int k = 0; k < size; k++) {
            outputArray[k] = initialArray[k];
        }
       // initialArray = outputArray;
        if (isThereDublication(outputArray)) {
            removeAllDuplications(outputArray);
        }
        return Arrays.toString(outputArray);
    }

    protected static boolean isThereDublication(int[] initialArray) {
        for (int i = 0; i < initialArray.length - 1; i++) {
            for (int j = i + 1; j < initialArray.length - 1; j++) {
                if ((initialArray[i] == initialArray[j]) && (i != j)) {
                    return true;
                }
            }
        }
        return false;
    }
}

Open in new window


package TaskChAll;



public class Taskv4Test_interestingBUG2 {

    public static void main(String[] args) {
        testRemoveAllDuplications();
    }

    private static void testRemoveAllDuplications() {
        System.out.println("To pass the test enter number of elements '5'");
        int[] vectorWithDuplication = {4, 2, 53, 2, 53,53,53};

       //  String  expectedTestResults = "{4, 2, 53}";
        

        System.out.println(Taskv4_interestingBUG.removeAllDuplications(vectorWithDuplication)); 

       

    }
}

Open in new window

0
Comment
Question by:SunnyX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:James Bilous
ID: 41765783
What are the symptoms of your problem? What is going wrong?
0
 

Author Comment

by:SunnyX
ID: 41765804
the output : [4, 2, 53, 53]

I don't know, I never saw such before. On debugging the code doing everything that I expect every assign  right output to the "outputArray" array. However, somehow reassign on the wrong one and return it. Please, copy past this code to the ide ( I tried intellij idea and eclipse both show wrong result ) take a look what happen during debugging , quite interesting.
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 350 total points
ID: 41765811
When you call removeAllDuplications() again, you're modifying the array you're passing into it, but you're not keeping track of the updated size of the array, so you're never actually discarding that last duplicate value.

Edit: Maybe.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 150 total points
ID: 41766342
There are 3 problems with your code above...

- CPColin has found one of them, yes you have a call to the removeAllDuplications method (line 28) but you AREN'T doing anything with the return value. Add a   return   keyword in front of line 28 so that you return the String that it generates.

 - Now that alone won't change anything though. Why? Because in this particular test case, you aren't actually calling removeAllDuplicates the second time. That's because there is a problem in your isThereDublication method. You AREN'T checking the last element in the array and so it never sees the two 53's.  You need to remove the   " - 1"   after initialArray.length from the inner loop (line 25)

 - The above two steps will resolve the issues HOWEVER, I assume that you went to the trouble of doing the recursive removeAllDuplicates call because the first loop wasn't fully removing all duplicates. The EASIEST way to fix all this is to just do it properly in the first place. If you add a line     j = i;     just after line 18 above, your j variable will do the right thing and you will remove ALL the duplicates in one go. You can then delete lines 27 - 29, and then delete the extra method at lines 33 - 42.
1
 

Author Closing Comment

by:SunnyX
ID: 41778274
Thx!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question