I have an array ordered in alphabetical order;
[0] = apple
[1] = banana
[2] = mango
[2] = melon
What I need to do now, Is split the string array into smaller string arrays with groups of the letters, so the output would be:
[0] = apple
开发者_C百科[0] = banana
[0] = mango
[1] = melon
I've tried a few methods, but all of them are hopeless, Can you give me a piece of code that will do this? I promise to give the best answer a tick and all the good answers a point!
Here's how I would do :
create a sorted map (for example a TreeMap) with the first char as key, and a list of fruits as value
iterate through the original array.
at each iteration, extract the first char and see if the map contains it as a key. If not, create an empty list of fruits, and put it in the map. Put the current fruit in the list (whether it was already in the map or not)
Ask for the values of the map : it's an ordered collection of fruit lists. transforming it into an array of arrays is trivial with Collection.toArray.
Here is a simple but not thoroughly optimized example. Also I'm not sure how this will fare with multi-byte first characters as in Umlauts etc.
public static void sortByFirstChar() {
String[] array = new String[4];
array[0] = "apple";
array[1] = "banana";
array[2] = "mango";
array[3] = "melon";
HashMap<Character, ArrayList<String>> charToList = new HashMap<Character, ArrayList<String>>();
for (String item : array) {
char firstChar = item.charAt(0);
if (charToList.containsKey(firstChar)) {
charToList.get(firstChar).add(item);
} else {
ArrayList<String> list = new ArrayList<String>();
list.add(item);
charToList.put (firstChar, list);
}
}
Set<Character> keySet = charToList.keySet();
for (char key : keySet) {
// Here are the arrays
System.out.println("Items for char " + new Character((char)key).toString() + ":");
for (String item : charToList.get(key)) {
System.out.println (" " + item);
}
}
}
Sample output:
Items for char b:
banana
Items for char a:
apple
Items for char m:
mango
melon
You will definitely want to use a better way to store the data instead of arrays... maybe a TreeMap or just a List of strings:
String[] arr = new String[]{"apple", "banana", "mango", "melon"};
List<List<String>> arrs = new ArrayList<List<String>>();
char f = 0;
List<String> last = null;
for(String s : arr){
if( f != s.charAt(0) ){
f = s.charAt(0);
// since first the char is different, create a new array
last = new ArrayList<String>();
last.add(s);
if( last != null ){
arrs.add(last);
}
}
else {
last.add(s);
}
}
In the case above, you will have a List of Lists of strings (arrs
). Good thing about lists is that their size is dynamic (arrays dimensions are static).
Here's an UNOPTIMIZED solution. I tested for a few different combos. The output is list of arrays. The printList function prints the array in logCat you might want to replace it with your own function:
public class SortArray extends Activity {
ArrayList matchedFruits = new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String fruits[] = new String[7];//Sorted array
fruits[0] = "apple";
fruits[1] = "apricot";
fruits[2] = "banana";
fruits[3] = "mango";
fruits[4] = "melon";
fruits[5] = "pineapple";
fruits[6] = "peach";
char currChar=fruits[0].charAt(0);//Get first char of first element
boolean match=false;
int len=fruits.length;
List tmp = new ArrayList();
for(int i=1;i < len;i++)
{
Log.d("Comparing ", fruits[i].charAt(0)+","+currChar);
if (fruits[i].charAt(0)==currChar)
{
if (match==false)//new match?
{
match=true;//Reset search
tmp.clear();//clear existing items
tmp.add(fruits[i-1]);
Log.d("Started new list ", fruits[i-1]);
}
else
{
tmp.add(fruits[i-1]);
Log.d("Added to list ", fruits[i-1]);
}
}
else
{
match=false;
tmp.add(fruits[i-1]);
matchedFruits.add(tmp.toArray(new String[tmp.size()]));//add to final list
Log.d("Finished a list ", fruits[i-1]);
tmp.clear();//clear existing items
}
currChar=fruits[i].charAt(0);
}
tmp.add(fruits[len-1]);
matchedFruits.add(tmp.toArray(new String[tmp.size()]));//add left over items
printList();
}
void printList()
{
//Print the list
for(int i=0;i < matchedFruits.size();i++)
{
String tmp2[]= matchedFruits.get(i);
for (int j=0;j < tmp2.length;j++)
{
Log.d("Final list", "Array #"+i+"["+j+"],"+tmp2[j]);
}
}
}
}
精彩评论