Debra Simeroth wrote:I was under the impression that an array can only hold one type of data, e.g. all int, double, string, etc.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Debra Simeroth wrote:I have a set of arrays related to inventory. One int array each to store product number and item count, one double array each for cost and total value of items, and one string array holding the item name. I need to sort the items alphabetically according to the string, without interfering with the other information. I am not sure how to approach this. Should I convert each item and its associated data in to a string representation and then sort, or should I try to use a sort or comparator method. I am a beginner, taking a class in Java and am not sure which method is best. We have not covered this yet, and references in the textbook are incomplete up to this point. Thanks in advance for your help.
It compiles and prints in the way I want it but all the values are null and 0.
Your suggestions are appreciated.
Debra Simeroth wrote:WOO HOO! Worked! Now back to the original sort problem.... I will bend my brain a while and get back to you if I can't figure it out... We are using Java How to Program by Deitel and Deitel, (we just finished chapter 10) so far there are no sort functions covered in the text and we are supposed to sort by product name. Any tips on which way to go? An enhanced for loop?
Debra Simeroth wrote:WOO HOO! Worked! Now back to the original sort problem.... I will bend my brain a while and get back to you if I can't figure it out... We are using Java How to Program by Deitel and Deitel, (we just finished chapter 10) so far there are no sort functions covered in the text and we are supposed to sort by product name. Any tips on which way to go? An enhanced for loop?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:
Debra Simeroth wrote:WOO HOO! Worked! Now back to the original sort problem.... I will bend my brain a while and get back to you if I can't figure it out... We are using Java How to Program by Deitel and Deitel, (we just finished chapter 10) so far there are no sort functions covered in the text and we are supposed to sort by product name. Any tips on which way to go? An enhanced for loop?
Now that you have one single array with Item objects, there are two ways:
1) make Item implement Comparable<Item>:
You can then use Arrays.sort(itemObjects);
2) Use a custom Comparator<Item>. The comparison code will basically be similar to my above code. You can then use Arrays.sort(itemObjects, comparator);
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:Well, if it is, then my code would surely result in a big fat F
[My Blog]
All roads lead to JavaRanch
Christophe Verré wrote:I didn't look at the whole code, but one thing caught my eyes : public interface Comparable
You don't have to make this interface. It's an interface from the API : java.lang.Comparable.
D. Ogranos wrote:
The Item class represents a single item. It would still be good to put any methods related to items there. In this case where you want to have a method work with an array of Items, you could use a static method in the Item class:
You then call this in your main program with
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|