aspose file tools*
The moose likes Java in General and the fly likes Adding and moving elements in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding and moving elements in an array" Watch "Adding and moving elements in an array" New topic
Author

Adding and moving elements in an array

Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Hi.so i have this code.it maintains records of Student details (name and ID).Now i need it to display the record alphabetically.The problem is, i am not allowed to use a sort method.i need to add a new student object to a position where it fits in alphabetically. for instance, if i already have "Felix Brown" at student[0] and i want to add "Alan shephard" to the array, i need to move "Felix Brown" to students[1] and place "Alan Shephard" in student[0]...here is my code. I need direction.Thanks in advance.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You need to TellTheDetails(←click) about what specific problem you're having. Not everyone here can read minds, and those of us that can have agreed not to use that power, so the others won't feel left out.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
i am not allowed to use a sort method


Why?

How about using a collection that automatically sorts, is that "allowed?"

Bill
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
It is very awkward to put elements into an array and sort them as you go. The suggestion about writing a collection which does its own sorting sounds a lot better. There are also some available ready‑made. You may need to find out about ordering objects. Fortunately the Java Tutorials has a section which covers just that.
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
William Brogden wrote:
i am not allowed to use a sort method



How about using a collection that automatically sorts, is that "allowed?"



No that's not allowed. i need to sort them as i add...
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Campbell Ritchie wrote: You may need to find out about ordering objects. Fortunately the Java Tutorials has a section which covers just that.


Am not supposed to sort it with collections.i know that's one way to do it,but the specification states otherwise.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Okay, so, what specific problem are you having?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I presume you know how to shift n elements one place up in an array? Hint: look at this method.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Wouldn't Daniel's methodology suffice here Jeff? The way Alan is describing his use-case, sorting as he adds without using a sort method, I am reminded of the problem that Daniel is solving currently.


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:Wouldn't Daniel's methodology suffice here Jeff?


I don't know who Daniel is, and I don't know what specific difficulty the OP is having in meeting his requirements, so I can't answer that, nor do I know if it's even relevant.
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Jeff I need to shift values in elements...Say i have student[0] = "Fred", if i add Anthony to the array, since Anthony comes before Fred, i want Anthony to replace Fred at student[0] and then move Fred to student[1]...and all these while am adding new student record.

Campbell Ritchie wouldn't the arrayCopy require me creating another array? that is a destination array?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jeff Verdegan wrote:
Mansukhdeep Thind wrote:Wouldn't Daniel's methodology suffice here Jeff?


I don't know who Daniel is, and I don't know what specific difficulty the OP is having in meeting his requirements, so I can't answer that, nor do I know if it's even relevant.


Daniel is the guy who is doing this assignment currently. You just replied saying his size was out of order.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Alan Anderson wrote:Jeff I need to shift values in elements...Say i have student[0] = "Fred", if i add Anthony to the array, since Anthony comes before Fred, i want Anthony to replace Fred at student[0] and then move Fred to student[1]...and all these while am adding new student record.

Campbell Ritchie wouldn't the arrayCopy require me creating another array? that is a destination array?


Break your problem into separate operations. As I see it, you need to check the name to be added with the existing names in the array and accordingly add the new entry at the respective index position. Am I correct? Is this the exact use case Alan?
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Mansukhdeep Thind wrote:
Alan Anderson wrote:Jeff I need to shift values in elements...Say i have student[0] = "Fred", if i add Anthony to the array, since Anthony comes before Fred, i want Anthony to replace Fred at student[0] and then move Fred to student[1]...and all these while am adding new student record.

Campbell Ritchie wouldn't the arrayCopy require me creating another array? that is a destination array?


As I see it, you need to check the name to be added with the existing names in the array and accordingly add the new entry at the respective index position. Am I correct? Is this the exact use case Alan?


Absolutely,that's what i need to do.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alan Anderson wrote:Jeff I need to shift values in elements...Say i have student[0] = "Fred", if i add Anthony to the array, since Anthony comes before Fred, i want Anthony to replace Fred at student[0] and then move Fred to student[1]...and all these while am adding new student record.


Yes, I know that. What I don't know is what problem you're having doing that.

Do you not know how to determine if Anthony belongs before Fred?

Do you not know how to put Fred into the next highest location?

Do you not know how to put Anthony into Fred's previous location?

Campbell Ritchie wouldn't the arrayCopy require me creating another array? that is a destination array?


What do the docs for System.arracopy have to say about it? (Campbell's a good guy and all, but I'd trust the javadocs more than I'd trust him on this if I were you. )
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
The answer to all your questions is yes.i don't know where to start.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alan Anderson wrote:The answer to all your questions is yes.i don't know where to start.


Okay, so take them one at a time.

Do you not know how to determine if Anthony belongs before Fred?


So you've got 2 Student objects, one with the name Anthony and one with the name Fred. Those names are Strings, right? So to see if the Student record for Anthony belongs before the student record for Fred, you need to:

1. Get the names from both of those records.

2. Compare the names to see which one is "less than" the other. That is, which one comes first in the list.

I assume you know how to do #1. For #2, look at the javadocs for java.lang.String(←click) and see if there's a method there that will help you. (Hint: Yes, yes there is. It does exactly what you want.)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Alan Anderson wrote:Campbell Ritchie wouldn't the arrayCopy require me creating another array? that is a destination array?

You need source and destination array references, but as I'm sure you know, two references can refer to the same object.
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
ok...let me get right to it.i'll be back
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Stuart A. Burkett wrote:] . . . two references can refer to the same object.
That is what I was going to say. I shall say something else:
Write an array with space at the end, and try some copying with arrayCopy. If you are not allowed to use collections, check that you are allowed to use arrayCopy.
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Okay...thanks
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Where have you reached with this assignment Alan?
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
No where...i just got back to it today
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Alan,
You have to be more specific about why your code and algorithm doesn't work.
Since you are not getting the "sorted" array results that you want, one place to be more specific is where the sorting is done in your algorithm.
Also, once you have some sorting in your code, you will have to be able to add a new student anywhere in the list, not just at the end.

The following code is where the student array is created and the new student is added.
When you create the array, it appears that you know the maximum number of students to add.
Some questions about this code:
1) When you add a student, you are currently adding the student at location "numOfStudents". What is the value of "numOfStudents on the first call to addStudent?
2) How do you know that location where you are adding a student is the "right" location?
3) What needs to occur when you need to add a student somewhere besides the end?

Can you write "tests" to check the above questions? Then work on the code till all the tests are satisfied. Also, you could add some print statement to the add function to print the location that is being added, and the items currently at that location and the prior location and the next location so you can see where the code is adding the student.



Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
Ok thanks.i'll get to it
Ojonugwa Ochalifu
Ranch Hand

Joined: Feb 08, 2011
Posts: 62
It's okay...i actually solved it using the bubble sort method...can i post the code here?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Yes, you can post the code here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding and moving elements in an array