Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Remove an element from an array ?

    Hi

    Im coding an array of Student objects.

    It will build have options to a) add and b) remove elements (Students) from the array.

    Adding is straightforward enough but the problem for me is removing.

    I assume the steps i need re something like :

    Search for matching object in array
    Remove object and move everything after back one
    Shrink array size by 1

    Problem is i believe you cant resize an array so maybe another array will be needed. Any suggestions?

    Thanks in advance.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you need to keep it sequential and compact in the array, and sized only large enough to hold the items within it, then you'd be better of with a second array. Simply iterate through the array that exists, and push onto the new array. If you hit the matching item to remove, then skip it, and keep going. If the end result is that no item was removed, than return the original, otherwise return the new.
    This said, if you keep only an array large enough to hold a given number of items at the time, than its not really optimized. It's quite a bit of effort to copy an array into a larger one, than it is to leave unused space.
    Compare it to the add. The remove should be pretty much the exact opposite. If you create an array of current.length + 1, then add an item to it, then it makes sense to do the same with remove. If the add is not always incrementing the current array and creating a new one, than I'd suggest that the remove should not and instead simply setting the offset when found to null.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks for the advice.

    Lets say for example after removing it i had to print it out.

    Code:
    1st set max students in class
    
    menu
    
    1 Add student
    2 Remove student
    3 Display list of students
    4 Exit
    lets say i set max to 3 then add 3 students at element 0,1,2

    then remove element 1.

    I need to be able to go back and enter another student

    Maybe somehow when printing skip anything thats null??
    but how will the add student function know where to put the student back if it doesnt free up the end?

    Maybe move all back one and leave the end null

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I think I see what you're getting at here.
    Is this for an assignment? If so, you'll want to go over the instructions very carefully. If it specifies that add must append to an array or insert at the end, then yes you'll need to figure out the best way of shuffling back. If it does not specify this, and it does not specify a sort order for the array, than I'd suggest insertion via iteration to find the first null entry. I would perceive this currently as tracking a count of the known values, and adding based on the known number of records.
    You can shift back the array as well. Its a matter of determining what you want to remove, and then from that point on reassigning the values back by one (or going the other way and looking forward). You'll need to deal with the null insertion at the end though, otherwise it would replicate the last value in the list. So you can do this within a single loop iteration as well.

    If this isn't for an assignment, than you don't deal with arrays at all. You deal with a collection designed for volume insert/remove operations on it. Suggested collection types would be LinkedLists if order doesn't matter (ie in order only), or a Tree if order is important. There is no built in Tree ADT in Java though, so you'd need to write one (or find one online). Linked types are the most ideal since you don't need to do anything special to free up space. Simply remove the item, and insert wherever.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts
    We actually have a working example where we cant see the source code so i cant check 100% for sure what way it has to be done. Its definitely arrays alright as thats what where covering now.

    Searching for the first null and putting the new entry in sounds like the most efficient way of doing it.

    Is there a way to just print out elements that are not null?

    I can send you the example if you want i just dont want to share it with every person who views this thread as its an assignment

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yeah, printing them out is simply a matter of using an if:
    PHP Code:
    for (Student s studentArray)
    {
        if (
    != null)
        {
            
    System.out.println(s.toString());
        }

    Unless the array is primitive, you must use a null check whether you use while, for or foreach style syntax. Otherwise it will issue a null pointer exception when you attempt to print it out.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts
    This did the trick.

    Code:
    int position = searchStudents(studentIDIn);
    		
    		if (position  == -1){
    			
    			return false;
    			
    		}
    
    		else{
    
    			for(int i = position  ; i <= (totalStudents-2) ; i++){
    
    				list[i] = (list[i+1]);
    			}
    			totalStudents--;
    			return true;
    		}	
    		}


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •