-2

Lets say that I have an array as followed :

  String[] letter = {a, b, c, e, f, }

How can I trim this array in order to get rid the empty element?

Yuri
  • 2,008
  • 17
  • 36
Best
  • 2,200
  • 9
  • 30
  • 49
  • Duplicate of: http://stackoverflow.com/questions/4150233/remove-null-value-from-string-array-in-java – Yuri Dec 01 '11 at 13:05
  • 1
    This question is not a duplicate of that one. – Erick Robertson Dec 01 '11 at 13:06
  • @Erick Robertson if you read the comments underneath that question, you will find that it is. – Yuri Dec 01 '11 at 13:06
  • Trailing comma in initializer is ignored. See [Here][1] [1]: http://stackoverflow.com/questions/3850203/java-array-initialization-list-ending-with-a-comma – Jeff Miller Dec 01 '11 at 13:08
  • 1
    -1 The question is unclear about what an empty element is. It seems to me that the OP believes a null is being added to the end of the array because of the trailing comma. But this is unclear, and the question needs to be improved. – Erick Robertson Dec 01 '11 at 13:12
  • @Erick Robertson: Valid explanation of the confusion :). – Yuri Dec 01 '11 at 13:13

5 Answers5

7

There is no "empty" element in that array. The trailing comma makes no difference - your array still has 5 elements.

If you have a real problem in real code where some elements in an array are empty (and you'll have to say what you mean by that - null value? Value referring to an empty string?) you can't change the size of an existing array, but you can create a new array, e.g. with

List<String> list = new ArrayList<String>();
for (String text : array)
{
    if (text != null && text.length() > 0)
    {
        list.add(text);
    }
}
array = list.toArray(new String[0]);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

There is no empty element.

Java allows for a trailing comma after the last element in an array defined in code. So there is no empty element in your array.

In order to create an empty (null) element, you would need to do this:

String[] letter = {a, b, c, e, f, null};

Note: It is my interpretation that the OP feels that the trailing comma is adding a null element to the end of the array.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
1

No Empty Element in your array. If you put like below then it will contains null and space also.

String[] letter = {"a", "b", "c", "e", "f", null, " h", " "};

and then you have to go through like this.

    for (int i = 0; i < letter.length; i++) {
        if (letter[i] != null && !letter[i].equals(" ")) {
            System.out.println("Letters::::::" + letter[i]);
        }

    }
    System.out.println("Length:::::" + letter.length);
subodh
  • 6,136
  • 12
  • 51
  • 73
1

Assuming that you did have an array with one or more null elements at the end, you could use the code below to find the index of the first null element and then create a copy of the array, discarding that element and anything after it.

// sample array to be trimmed
String[] array = {"1", "2", "3", null, null};

int end = Arrays.asList(array).indexOf(null);
if (end >= 0) {
    String[] tmp = new String[end];
    System.arraycopy(array, 0, tmp, 0, end);
    array = tmp;
}
ewan.chalmers
  • 16,145
  • 43
  • 60
0

You can use this simple code snippet to delete nulls and empty strings from array` import java.util.Arrays;

public class Test {
   public static void main(String[] args) {
      String data = "1, 2, ,4 , , 5 ";
      String[] split = data.split(",");
      split = Arrays.stream(split).filter(s -> (s != null && s.length() > 0 && !s.trim().equals("")))
            .toArray(String[]::new);
      for(String str:split){
          System.out.println(str.trim());
      }
  }
}`
Venu Morigadi
  • 589
  • 6
  • 4