fork(1) download
  1. class RemoveBlanks {
  2. public static void main(String[] args) {
  3. // Get results.
  4. String[] newArr = results(new String[] { null, null, null, "Mars", "Saturn", "Mars" });
  5.  
  6. // Print results.
  7. for (String elem : newArr) {
  8. System.out.println(elem);
  9. }
  10. }
  11.  
  12. private static String[] results(String[] allElements) {
  13. // Calculate the size for the new array.
  14. int newSize = 0;
  15. for (int i = 0; i < allElements.length; i++) {
  16. if (allElements[i] != null) {
  17. newSize++;
  18. }
  19. }
  20.  
  21. // Populate the new array.
  22. String[] _localAllElements = new String[newSize];
  23. int newIndex = 0;
  24. for (int i = 0; i < allElements.length; i++) {
  25. if (allElements[i] != null) {
  26. _localAllElements[newIndex] = allElements[i];
  27. newIndex++;
  28. }
  29. }
  30.  
  31. // Return the new array.
  32. return _localAllElements;
  33. }
  34. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
Mars
Saturn
Mars