-2

I have an array like this (source)

[Maria Carolina, Luisa Joana, Lara Silva, Catarina Patricio, Paula Castro, fim, null, null, null]

and I want an array like that (destination)

[Maria Carolina, Luisa Joana, Lara Silva, Catarina Patricio, Paula Castro]

In the following code i is the number of names.
My code is:

String[] nomeCompleto = new String[10];
String[] nomes = new String[10-i];
if(i < 10) {
    nomes[i] = nomeCompleto[i];
}
System.out.println(Arrays.toString(nomes));

return;

What am I doing wrong?

zx485
  • 28,498
  • 28
  • 50
  • 59
Ruca
  • 17
  • 4

2 Answers2

-1

Edit: The question presents a source code fragment based on array indices, hence my original answer:

Use the Arrays class to do this for you.

String[] names = new String[] {"Maria Carolina", "Luisa Joana", "Lara Silva", "Catarina Patricio", "Paula Castro", "fim", null, null, null};
String[] truncated = java.util.Arrays.copyOf(names, names.length-4); // remove the last 4 names
System.out.println(java.util.Arrays.toString(truncated));

Try it online here.

Edit: Since people (not the OP) weren't too happy with that, I added: Or, to match only names of the form Firstname Lastname, use a regex:

String[] input = new String[] {"Maria Carolina", "Luisa Joana", "Lara Silva", "Catarina Patricio", "Paula Castro", "fim", null, null, null};
List<String> namesList = new ArrayList<>();
for(String name : input) {
    if(name != null && name.matches("^[A-Z][A-z]+ [A-Z][a-z]+$"))
        namesList.add(name);
}
String[] namesArray = namesList.toArray(new String[0]);
System.out.println(Arrays.toString(namesArray));

Try it online here.

Edit: Finally, since Dukeling commented on fim meaning end in Portuguese, a better solution might be:

Use a loop to find the first occurrence of fim and then truncate the array accordingly (as in the first code snippet in my answer).

String[] names = new String[] {"Maria Carolina", "Luisa Joana", "Lara Silva", "Catarina Patricio", "Paula Castro", "fim", null, null, null};
int newLength = names.length;
for(int i = 0; i < names.length; i++) {
    if("fim".equals(names[i])) {
        newLength = i;
        break;
    }
}
String[] truncated = java.util.Arrays.copyOf(names, newLength);
System.out.println(java.util.Arrays.toString(truncated));

Try it online here.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
  • does it remove "fim" – nits.kk Mar 24 '18 at 18:18
  • @nits.kk Have you clicked the link in my answer? Yes, it does remove `fim`. @everyoneelse I would appreciate some input on how I can improve my answer since you guys have downvoted. – O.O.Balance Mar 24 '18 at 18:21
  • Although I have not down-voted it, but still above solution assumes "fim" to be at the end of the array, which may or may not be the case. – nits.kk Mar 24 '18 at 18:22
  • This solution does literally what the OP asked – for the given input. One could use a regex – I will expand my answer. – O.O.Balance Mar 24 '18 at 18:34
  • @nits.kk "fim" is Portuguese for "end", and having a special character to indicate the end of an array is not unheard of (even if it seems unnecessary and error-prone here done like this). Assuming it's the end of the array seems like a reasonable assumption. – Bernhard Barker Mar 24 '18 at 18:45
  • @Dukeling thanks for the info, its reasonable and answer is good so a upvote for the same from my end. – nits.kk Mar 24 '18 at 18:50
-1

As per the Question's understanding Problem statement is as follows

Given a master array of String we need to return another array which contains the elements of master with null elements and element with value "fim" removed.

There can be null elements in between and not neccessary in the last and same for the element "fim"

Basic Algorithm

  1. iterate master array
  2. count the number of null & fim elements, create copy array of size = master array size - null elements count
  3. Check if the element is null if not null then add to copy array

    public String[] removeNullNFimElementsFromArray(String[] master) {
        int nullNFimCount = 0;
        int masterSize = master.length;
        for(int i = 0; i < masterSize; i++) {
            if(master[i] == null || "fim".equals(master[i])) {
                nullNFimCount++;
            }
        }
        int copySize = masterSize - nullNFimCount;
        String[] copyArray = new String[copySize];
        for(int i = 0, j = 0; i < masterSize; i++) {
            if(master[i] != null && !"fim".equals(master[i])) {
                copyArray[j] = master[i];
                j++;
            }
        }
        return copyArray;
    }
    
nits.kk
  • 5,204
  • 4
  • 33
  • 55