Shorten array length once element is remove in Java.

Posted by lupin on Stack Overflow See other posts from Stack Overflow or by lupin
Published on 2010-05-06T01:00:20Z Indexed on 2010/05/06 1:08 UTC
Read the original article Hit count: 410

Filed under:
|
|
|
|

Note: Following is my homework/assignment, feel free not to answer if you will.

I want to delete/remove an element from an String array(Set) basic, I'm not allowed to use Collections..etc.

Now I have this:

void remove(String newValue) {

            for ( int i = 0; i < setElements.length; i++) {
               if ( setElements[i] == newValue ) {
                    setElements[i] = "";

               }
            }

       }   

I does what I want as it remove the element from an array but it doesn't shorten the length. The following is the output, basically it remove the element indexed #1.

D:\javaprojects>java SetsDemo
Enter string element to be added
A
You entered A
Set size is: 5
Member elements on index: 0 A
Member elements on index: 1 b
Member elements on index: 2 hello
Member elements on index: 3 world
Member elements on index: 4 six
Set size is: 5
Member elements on index: 0 A
Member elements on index: 1
Member elements on index: 2 hello
Member elements on index: 3 world
Member elements on index: 4 six

lupin

© Stack Overflow or respective owner

Related posts about java

Related posts about array