Array newbie - some pointers please
I learn best by example, but unfortunately haven't as yet found anything of use on the internet that has led me to a solution to the problems i'm having with my files.
I'm expanding a little on the three class files held in the zip file attached started by someone else. I would like to go about searching, sorting and filtering the data by any one of the columns shown. Example data can be input by running the seller program and selecting option r.
If anyone could point me in the direction of some useful literature with helpful examples it'd be much appreciated!
[604 byte] By [
pcdj] at [2007-11-11 8:02:45]

# 1 Re: Array newbie - some pointers please
Can you be more specific to your problem? If you're searching for an element in an array, just loop through it.
Object itemToSearchFor;
// ...
for (int i = 0; i < someArray.length; i++) {
if (itemToSearchFor.equals(someArray[i])) {
// we found a match
}
}
For sorting, there are many ways. If you want to write your own, I would go with bubble sorting (http://leepoint.net/notes-java/data/arrays/32arraybubblesort.html) because although it is inneficient, it is easy to implement. The easiest way is just to use the Arrays class's static sort method.
Arrays.sort(someArray);
Hope this helps.
destin at 2007-11-11 22:36:41 >

# 2 Re: Array newbie - some pointers please
Thanks. I shall have a play around with that code and see how it would fit in.
With regards to the sorting, this is quite complicated to explain without knowing the correct jargon... in my example each array position holds a number of fields (title, author, price, year etc). As I understand it any sort function will only sort by the first piece of data in the array (ie. I would only be able to sort by title - but what if I wanted to sort by year?).
pcdj at 2007-11-11 22:37:35 >

# 3 Re: Array newbie - some pointers please
Are you required to use an Array? With collections which implement the Comparator interface, you can establish your own meaning to what is sorted order of the stored elements, and how your collection will be sorted.
You are in luck, if you must use an array, because the Arrays class provides sort methods for arrays, and you can implement a Comparator for your array. You can provide several comparators for your array and then choose one with your option selection to determine whether you are ordering based on any one of your data fields.
nspils at 2007-11-11 22:38:39 >
