3

I have an Arraylist letters which contains letter f r t d, and also two more Arraylists one of which stores the row poistion of a letter and the other stores the col position for a letter. But after sorting the Arraylist letters alphabetically (Collections.sort(letters)) how can I also sort the row and col values for each letter correspondingly? For example, if the letter d has row = 4and col ==3 then after sorting d would be in the first place and takes the row and ``col values of f. How can I do that if letters get their row and col values.

Given:

ArrayList<Character> letters;
ArrayList<Integer> rows;
ArrayList<Integer> cols;

Snippet:

Collections.sort(letters);
for(int i = 0; i < letters.size(); i++)
{
   System.out.println(letters.get(i).getChar() + rows.get(i) + col.get(i)
}
3
  • 2
    Do you have to use a List? Looks like Map will be a better option. Since you are Mapping the letter to the rows and columns. Commented Apr 28, 2015 at 19:17
  • Perhaps you're using the wrong data structure? Commented Apr 28, 2015 at 19:19
  • rgettman's answer has the right idea. It would be best to encapsulate the relationship between letter, row and col in a new class. Commented Apr 28, 2015 at 19:33

2 Answers 2

5

The answer is not to sort individual arrays that each store one element of each character.

Create your own class to hold the 3 associated data attributes together, e.g. Letter.

Do one of the following:

  • Have the class implement Comparable<Letter>, adding a compareTo method. Then have an ArrayList of Letters on which you can call Collections.sort.
  • Create another class that implements Comparator<Letter> with a compare method. With your ArrayList of Letters, call Collections.sort passing in an additional parameter that is an instance of your Comparator.
Sign up to request clarification or add additional context in comments.

7 Comments

but how could I assign the corresponding rows and cols to letters.
Your Letter class would contain attributes for the row and column as well as the actual character.
Can I use TreeMap like TreeMap<String, Integer, Intger> ?, @rgettman
No, TreeMap has only 2 type parameters. But you could have a TreeMap whose key is a Character and whose value is some pair of Integers. It's also possible to maintain a TreeSet<Letter>. That would be best if your list of characters is unique and it changes over time, but it also needs to remain sorted at all times. But if you only need to sort it once, having an ArrayList and a call to Collections.sort will work just fine. Either way, either Letter should be Comparable<Letter> or you will also create another class that implements Comparator<Letter>.
Thanks for reply, @rgettman, but for TreeMap how could I set up pair of Integers ? Something line TreeMap<Character, Integer, Integer> temp ?
|
0

IMHO, this could not be achieved out-of-box, you can achieve it in two ways:

  1. Using any suitable sorting technique (Bubble, Merge, Quick etc), sort them manually. Process all three togather, stop when you get letters in desired order.

  2. Create a Map, key would be your letter, value will be String (rowNum_colNum, or use any separator between them) and sort that map by keys. Later, break the value and retrieve both the numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.