Array newbie - some pointers please


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Array newbie - some pointers please

  1. #1
    Join Date
    Feb 2006
    Posts
    2

    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!
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Can you be more specific to your problem? If you're searching for an element in an array, just loop through it.
    Code:
    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 because although it is inneficient, it is easy to implement. The easiest way is just to use the Arrays class's static sort method.
    Code:
    Arrays.sort(someArray);
    Hope this helps.

  3. #3
    Join Date
    Feb 2006
    Posts
    2
    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?).

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2006, 05:51 PM
  2. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 07:16 AM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 10:05 AM
  4. Replies: 2
    Last Post: 04-15-2005, 10:06 PM
  5. newbie - Array of objects
    By Mary in forum Java
    Replies: 1
    Last Post: 02-16-2001, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center