Help with java arrays

2

I have the array disciplines and the current array.

The current one is an exact copy of the subjects. If there are two equal strings within the disciplines array I need to delete all equal and keep only one.

That is, the current should not contain repeated strings.

Can anyone help with this code?

String coisa = "";
String[] atual = disciplina.clone();;
int conta = 0;
for (int j = 0; j < disciplina.length; j++) {
  if (disciplina[j] != null && nota[j] != 0) {
      if(atual[j].equals(disciplina[j])){
        atual[j] = "";
      }
  }
    
asked by anonymous 16.11.2017 / 17:31

3 answers

2
public static void main(String[] args) {
    String[] a = {"teste1", "teste2", "teste1"};
    String[] b = new String[a.length];
    for(int i = 0; i < a.length; i++) {
        if(arrayNaoContemString(b, a[i])) {
            b[i] = a[i]; 
        }
    }
    for (String string : b) {
        System.out.println(string);
    }
}

public static boolean arrayNaoContemString(String[] array, String string){
   for(String s : array){
       if (string.equals(s)){
           return false;
       }
   }
   return true;
}

View the result on Ideone . Or see this question in SO-en for more solutions.

    
16.11.2017 / 18:27
3

You can use Set that does not allow duplicates.

Set<String> disciplinas = new HashSet<>(Arrays.asList(disciplina));

disciplina = disciplinas.toArray(new String[disciplinas.size()]);
  

Set

     

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals (e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Free translation:

  

A collection that does not contain duplicate items. More formally, sets do not contain pairs of elements e1 and e2 in case of e1.equals (e2). As the name suggests, this interface is an abstraction of the mathematical model "set".

A suggestion for resolution without using Collection :

public String[] removerDuplicados(String[] base) {
  String[] resultado = new String[base.length];
  int contador = 0;
  boolean encontrado;

  for (int i = 0; i < base.length; i++) {
    String elemento = base[i];

    encontrado = false;

    for (int j = 0; j < resultado.length; j++) {
      if (resultado[j] == null) {
        break;
      }

      if (resultado[j].equals(elemento)) {
        encontrado = true;
      }
    }

    if (!encontrado) {
      resultado[contador] = elemento;
      contador++;
    }
  }

  return Arrays.copyOfRange(resultado, 0, contador);
}

Another way is to sort array and then just compare to the previous record to insert into another, without the duplicates:

public String[] removerDuplicados(String[] base) {
  String[] resultado = new String[base.length];
  String anterior = null;
  int indice = 0;

  Arrays.sort(base);

  for (String atual : base) {
    if (atual != null && (anterior == null || !atual.equals(anterior))) {
      resultado[indice] = atual;
      indice++;
    }

    anterior = atual;
  }

  return Arrays.copyOfRange(resultado, 0, indice);
}

Apply the above two methods as follows:

disciplina = removerDuplicados(disciplina);
    
16.11.2017 / 17:36
1

Try this:

if (!atual.contains(disciplina[j])){
    atual.add(disciplina[j])
}

So if the array atual does not contain the contents of discplina[j] it will add

    
16.11.2017 / 17:36