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
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?
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
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
Yeah, printing them out is simply a matter of using an if:
PHP Code:
for (Student s : studentArray) { if (s != 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