Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-02-2013, 11:09 PM   PM User | #1
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
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.
trancecommunity is offline   Reply With Quote
Old 03-03-2013, 01:55 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,657
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-03-2013, 11:38 AM   PM User | #3
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
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
trancecommunity is offline   Reply With Quote
Old 03-03-2013, 03:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,657
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-03-2013, 03:39 PM   PM User | #5
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
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
trancecommunity is offline   Reply With Quote
Old 03-03-2013, 10:04 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,657
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-04-2013, 11:02 PM   PM User | #7
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
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;
		}	
		}
trancecommunity is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:39 PM.


Advertisement
Log in to turn off these ads.