Results 1 to 3 of 3
- 02-02-2009, 08:54 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
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++;
}
}
- 02-02-2009, 09:33 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you tel me how do you add three elements in the same ArrayList, I mean name, phone number and address?
- 02-02-2009, 11:29 AM #3
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
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks