Hi folks:
I need help from any Java guru, for this little program. This is homework I've done without success, but I want to understand where is my error and how to solve this exercise.
The problem is:
Giving an array of ints (with possible duplicate elements) I want to list pairs of numbers in ascending order without repetitions.
The classes Sequence and FixedSize can't be changed, because they are inherited by other classes (external to this code) and they ran without errors.
The only class that may (and need to) be changed is SortArray.
Here's the code:
public abstract class Sequence { private int index = -1; public final int nextIndex() { return index+1; } public boolean hasNext() { return nextIndex() < size(); } public int next() { return ++index; } public abstract int size(); } public abstract class FixedSize extends Sequence { private final int size; public FixedSize(int sz) { size = sz; } public final int size() { return size; } } public class SortArray extends FixedSize { public static int WithoutRepetitions[]; // static array where the static method 'RemoveDups' stores the result public SortArray(int a[], int bi, int ei) { super (RemoveDups (a, bi, ei)); // super() calls a static method that receives the array of integers, // removes duplicates and return the number of elements without repetitions } private int idx = 0; // variable to store the n-th element of the array @Override public int next() { super.next(); return WithoutRepetitions[idx++]; // return the n-th element of the array // and increment the index to the next element // to be shown in the next call to next() } // static method for removing duplicates (must be static to pass as argument of super() ) private static int RemoveDups(int a[], int bi, int ei) { int[] ArrayRange = Arrays.copyOfRange(a, bi, ei); // sorting array with repetitions Arrays.sort(ArrayRange, 0, ArrayRange.length); // temporary array to store non duplicate elements int tmp[] = new int[ArrayRange.length]; boolean dups; tmp[0] = ArrayRange[0]; int DimArray = 0; for (int i=1; i<ArrayRange.length; i++) { dups= false; for (int j=0; j<tmp.length; j++) { if (ArrayRange[i] == tmp[j]) dups= true; } if (!dups) { DimArray++; tmp[DimArray] = ArrayRange[i]; } } DimArray++; // size of array without repeated elements // copy to array static array non duplicated elements WithoutRepetitions = Arrays.copyOf(tmp, DimArray); return DimArray; // return number of elements of array > to use in super() - (see above) } }
When we ran the code snippet:
public static void main(String[] args) { int a[] = {7, 5, 8, 5, 2, 1}; Sequence s1 = new SortArray(a, 1, 6), s2 = new SortArray(a, 3, 5); while (s1.hasNext()) System.out.print(s1.nextIndex() + ":" + s1.next() + " - "); while (s2.hasNext()) System.out.print(" - " + s2.nextIndex() + ":" + s2.next()); }
The expected output should be:
0:1 - 1:2 - 2:5 - 3:8 - - 0:2 - 1:5 (two sequences of numbers - one for each while - in ascending order without repetitions)
However this code doesn't run, because the static array "WithoutRepetitions[]" being static is shared by the two instances of Sequence (s1 and s2),
and both while loops show the second sequence (0:2 - 1:5) crashing the program in the first while because the length of s1 is bigger than the length of s2.
If I exchange the order of instructions like below, it works:
public static void main(String[] args) { int a[] = {7, 5, 8, 5, 2, 1}; Sequence s1 = new SortArray(a, 1, 6); while (s1.hasNext()) System.out.print(s1.nextIndex() + ":" + s1.next() + " - "); Sequence s2 = new SortArray(a, 3, 5); while (s2.hasNext()) System.out.print(" - " + s2.nextIndex() + ":" + s2.next()); }
In that order it works, however I'm not allowded to do this:
Somebody advice me to instead of a static array, I must use an instance array, however this is not possible, because the method "RemoveDups" must be static to be used inside super() and because of this constraint (static) can't access non static variables.
Can someone help me to solve this.
Thank you in advance for your help.