1

I made this code. I think it is wrong.

public void display() {
    for (int i = 0; i < tabT.length; i++)
        if (tabT[i] != null)
            for (int j = 0; j <= i; j++)
                if (tabT[i] != tabT[j])
                    System.out.println(tabT[i].getCar());
}

How do I display elements without redundancy in an array?

Roman C
  • 49,761
  • 33
  • 66
  • 176
mpluse
  • 1,857
  • 6
  • 18
  • 25

6 Answers6

1

Objects compared via equals() For example

if (!tabT[i].equals(tabT[j]))

you are comparing the references values not the objects

for (int i=0; i< tabT.length; i++) {
  boolean f = false;
  for (int j=i+1; j <tabT.length; j++)
    if (tabT[i].equals(tabT[j])) {
      f=true;
      break;
    }
  if (!f)
    System.out.println(tabT[i].getCar());
}

this should give you all combinations non-repeating for i and j, so we don't compare them multiple times.

Roman C
  • 49,761
  • 33
  • 66
  • 176
1

If you want to use only arrays, you can do something like this:

Make an temp (helper) array, which will include each element seen so far in tabT. Then, before printing the value, you check if it's not appearing in the helper array (tmp).

For example, if you have values in tabT, and you don't want to print each one more than once:

int[] tabT = {1,2,3,1,1,2,6,7,2,7,1};
int[] tmp = new int[tabT.length];
boolean flag;
for (int i = 0; i < tabT.length; i++) {
    tmp[i] = tabT[i];
    flag = true;
    for (int j = 0; j < tmp.length; j++)
        if (tabT[i] == tmp[j] && i!=j) {
            flag = false;
        }
    if(flag)
        System.out.println(tabT[i]);
}

Output: [1,2,3,6,7]

You can easily apply this idea to your program, and you'll have each element printed only once:

Cars[] tmp = new Cars[tabT.length]; //Assuming tabT is from type Cars[]
boolean flag = true;
for (int i = 0; i < tabT.length; i++) { 
    tmp[i] = tabT[i];
    if (tabT[i] != null) {
        for (int j = 0; j < tmp.length; j++)
            if (tabT[i].getCar().equals(tabT[j].getCar()) && i!=j)
                flag = false;
        if(flag)
            System.out.println(tabT[i].getCar());
    }
}

This will print each car (or whatever you're printing) only once.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Thank you but "tmp" isn't empty here? And how can I remove the array then? – mpluse Mar 11 '13 at 20:49
  • There is a problem here. if (tabT[i].getCar().equals(tabT[j].getCar()) && i != j) – mpluse Mar 11 '13 at 21:05
  • You should know how to handle it. I gave you a very good idea of how to do it. It's your job to do little changes in order to make it work. I don't know about your classes and your variables. – Maroun Mar 11 '13 at 21:07
  • I think it's tmp[j].getCar().equals .. not tabT[i].getCar().equals – mpluse Mar 11 '13 at 21:16
  • I've found it in another way. Thank you anyway :) – mpluse Mar 12 '13 at 03:05
0

Put tabT array in a Set. There will be no duplicate items.

Set tabTList = new HashMap(Listjava.util.Arrays.asList(tabT);
Jean Waghetti
  • 4,711
  • 1
  • 18
  • 28
  • In the exercise, I should have duplicate items in the array but when I will display them, it should be without redundacy. – mpluse Mar 11 '13 at 20:31
  • @mpluse: You can always convert to a set to remove the duplicates and leave the original collection intact – Niklas B. Mar 11 '13 at 20:32
0

You could consider a HashMap if you do want to keep track of duplicate count. Iterate through array once to place objects into the HashMap with their respective counts. Then iterate through the array again checking against the HashMap. This would be O(n) time as opposed to potential O(n^2)

Franklin
  • 1,771
  • 3
  • 17
  • 35
0

== and != test equality at the object level (i.e. if both instances are the same). What you need is to compare the value represented by each object (e.g. if two strings are equals), then you need to ask whether !tabT[i].equals(tabT[j]), and make the elements of tabT implement equals).

Or convert the array to a set, which removes duplicates.

T[] tabT = ...
Set<T> set = new LinkedHashSet<T>(Arrays.asList(tabT))
for (T t:set) System.out.println(t);

I used a LinkedHashSet because it preserves the order of the elements in the array. Note that you need to implement equals and hashcode.

Javier
  • 12,100
  • 5
  • 46
  • 57
0

Why not trying something like this? (I'm supposing that you are working with String types)

HashSet<String> hashSet = new HashSet<String>();

for (int i = 0; i < tabT.length; i++) {
    hashSet.add(tabT[i]);
}

You can't have duplicates into a set, so now you can iterate the set to get the uniques.

java.util.Iterator<String> iterator = hashSet.iterator();

while (iterator.hasNext()) {
    System.out.println((String)iterator.next());
}
Daniel García Baena
  • 1,191
  • 4
  • 19
  • 33