#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2

    Remove null el. from array


    Hi . I need to write func. that removes null elements from array . But my func is wrong. How can i do that ?
    Here is my func :
    Code:
       	public static void nullDel(String[] strArr){
    		
    		List<String> asL= Arrays.asList(strArr);
    		List<String> list= new LinkedList<String>(asL);
    		
    		for(int i= 0; i< strArr.length; i++){
    			if(strArr[i] == null){
    				if(strArr[i].equals(null)){
    					list.remove(i);
    				}
    			}
    		}
    		finRecArray= (String[]) list.toArray(new String[list.size()]);
    		
    	}
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    --
    Last edited by bullet; September 3rd, 2013 at 12:35 PM.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I'd use the == operator to test if the contents of an array element is null. Why does the code have 2 tests?
    my func is wrong.
    Can you explain what is "wrong"?
    Are there errors?
    Does the code not do what you want it to do?

    Print the array's contents before and after the method call:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    You see . There is an error which oqures if i use only
    2nd statement :

    Exception in thread "main" java.lang.NullPointerException
    at Task.nullDel(Task.java:129)
    at Task.main(Task.java:37)

    And nullpointerexception

    if i use only 1st :

    (Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9, Size: 8
    at java.util.LinkedList.checkElementIndex(LinkedList.java:553)
    at java.util.LinkedList.remove(LinkedList.java:523)
    at Task.nullDel(Task.java:130)
    at Task.main(Task.java:37)

    becides my funtion doin nothing. i need it to remove all the nulls from input array .

    here is input array :
    1001-40
    1002-80
    1003-60
    1005-55
    1007-68
    1009-99
    1004-89
    null
    null
    null

    here is output:
    1001-40
    1002-80
    1003-60
    1005-55
    1007-68
    1009-99
    1004-89
    null
    null
    null
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    There is a problem when going through a list from index 0 to the end. When an item is removed from the list, the index values for all the rest of the items in the list are decrease by one.
    If the code goes the other way through the list from the last element to the first, the indexes of any of the unexamined elements doesn't change.

    If the null values are always at the end, why not just search for the last valid data element and create a new list with the valid elements.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Note these lines

    Code:
    if(strArr[i] == null){
    				if(strArr[i].equals(null)){
    					list.remove(i);
    				}
    			}
    If strArry[i] is null, then when you try to see if it is equal to the String null, you will get a NullPointerException.

    Try removing the inner if statement and just have list.remove
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    Thank you . I understand about nullpointerex. so i changed my check :
    Code:
    	public static void nullDel(String[] strArr){
    		
    		List<String> asL= Arrays.asList(strArr);
    		List<String> list= new LinkedList<String>(asL);
    		
    		for(int i= 0; i< strArr.length; i++){
    			if(strArr[i] != null){
    				if(strArr[i].equals(null)){
    					list.remove(i);
    				}
    			}
    		}
    		finRecArray= (String[]) list.toArray(new String[list.size()]);
    		
    	}
    But here , as i see , i don't have any results . because i check the element for not being null , and then not removing null element.
    so now i'll do the thing you said me : i'll check the element for not being null and then past it into the array.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    Thank you very much ! It's alive !!!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    Code:
    	public static void nullDel(String[] strArr){
    		
    		List<String> asL= Arrays.asList(strArr);
    		List<String> list= new LinkedList<String>(asL);
    		List<String> list1= new LinkedList<String>(asL);
    		
    		for(int i= 0; i< strArr.length; i++){
    			if(strArr[i] != null){				
    					list1.add(list.get(i))   ;				
    			}
    		}
    		finRecArray= (String[]) list1.toArray(new String[list1.size()]);
    		
    	}
    It should look like this ?

IMN logo majestic logo threadwatch logo seochat tools logo