Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding and moving elements in an array

 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so, what specific problem are you having?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you know how to shift n elements one place up in an array? Hint: look at this method.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to all your questions is yes.i don't know where to start.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...let me get right to it.i'll be back
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...thanks
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where have you reached with this assignment Alan?
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No where...i just got back to it today
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks.i'll get to it
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's okay...i actually solved it using the bubble sort method...can i post the code here?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can post the code here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic