Results 1 to 3 of 3
  1. #1
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default how to sort "name" arraylist with its phone & address?

    how do i sort the name so that its phone and add will also follow when i call list()?
    this is a phonebook.. please provide additional code.. :o (i don't want to combine name, phone and addr into one arraylist)
    public void addP(){

    name = JOptionPane.showInputDialog("Enter Name:");
    page.add(name);
    phone = JOptionPane.showInputDialog("Enter Phone:");
    num.add(phone);
    addr = JOptionPane.showInputDialog("Enter Address:");
    add.add(addr);
    i++;
    }

    public void list(){
    int minl = 1;

    for (int i = 0; i < page.size(); i++){
    System.out.println();
    System.out.println("Contact # "+ minl+":"+ "\t"+page.get(i));
    System.out.println("\t\t"+num.get(i));
    System.out.println("\t\t"+add.get(i));
    minl++;
    }

    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you tel me how do you add three elements in the same ArrayList, I mean name, phone number and address?

  3. #3
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Best solution would be to introduce a class PhonebookEntry with three properties name/phone/address and use it for sorting ( sort ArrayList<PhonebookEntry> ).

    Another approach would be to introduce "index" list which is initially filled with 0,1,2,3,4,5,6 etc. numbers. Then you sort this list using sort(T[] a, Comparator<? super T> c) , comparator must compare elements of Page list using passed values as index values. You can use sorted "index" list like this:

    Java Code:
    for (int i = 0; i < index.size(); i++){
      System.out.println();
      System.out.println("Contact # "+ minl+":"+ "\t"+page.get(index[i]));
      System.out.println("\t\t"+num.get(index[i]));
      System.out.println("\t\t"+add.get(index[i]));
      minl++;
    }
    Last edited by ProjectKaiser; 02-02-2009 at 11:31 AM.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  3. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •