1

我想对以下二维数组进行排序:

String[][] rows = {
            {"M","O","N","K","E","Y"},
            {"D","O","N","K","E","Y"},
            {"M","A","K","E"},
            {"M","U","C","K","Y"},
            {"C","O","O","K","Y"},
            };

我希望得到以下结果:

A C C D E E E K K K K K M M M N N O O O O U Y Y Y Y

但是我不知道该怎么做!!我找不到任何二维数组排序的好例子。

如您所见,我不想按列或行排序,但我只想对数组中的所有字符进行排序。通过这种方式,我可以计算每个字符中有多少位于 2D 数组中。

如果您知道如何计算 2D 数组中每个字符的数量,那对我来说也是一个很好的解决方案。

4

4 回答 4

2

这将使用 将二维数组排序为一个List对象Collections.sort

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        String[][] rows = {
                {"M","O","N","K","E","Y"},
                {"D","O","N","K","E","Y"},
                {"M","A","K","E"},
                {"M","U","C","K","Y"},
                {"C","O","O","K","Y"},
            };

        List<String> list = new ArrayList<String>();

        // Add all strings to list.
        for (String[] row : rows) {
            for (String s : row) {
                    list.add(s);
            }
        }

        Collections.sort(list); // Sort the list.
    }
}

您可以使用以下方法将列表转换为基本数组:

String[] arr = list.toArray(new String[list.size()]);

要计算数组中的每个字符,请查看jlordo 的答案。

于 2013-06-02T17:09:08.900 回答
1

我读到你想计算唯一的字符串?用这个:

    Map<String, Integer> counter = new HashMap<>();
    for (String[] row : rows) {
        for (String str : row) {
            if (counter.containsKey(str)) {
                counter.put(str, counter.get(str) + 1);
            } else {
                counter.put(str, 1);
            }
        }
    }
    System.out.println(counter);

如果您希望按字典顺序对输出进行排序,请使用 aTreeMap代替,如果您只有单个字符,请HashMap考虑使用Character代替。String

于 2013-06-02T17:05:32.123 回答
0

我知道你想要一个 2D 字符串数组中唯一字符的计数/每个字符串是 1 个字符,或者我们只考虑第一个。此代码假定您希望将“A”计数与“a”分开,否则将字符串大写作为注释掉。

如果您想要字符串数组(1D)中的字符,也可以这样做。以下是您的问题中的 2D

import java.util.*;
public class ChrCnt  {
    public static void main(String[] args){

        String[][] rows = {
            {"M","O","N","K","E","Y"},
            {"D","O","N","K","E","Y"},
            {"M","A","K","E"},
            {"M","U","C","K","Y"},
            {"C","O","O","K","Y"},
            };
            Map<Character, Integer> cnts = new HashMap<Character, Integer>();
            for(int i =0; i < rows.length; i++){
                for(int j =0; j < rows[i].length; j++){
                    Character c = rows[i][j].charAt(0);//or .toUpperCase().charAt(0);
                    Integer cnt = cnts.get(c);
                    int cc = 0;
                    if(cnt != null){
                        cc = cnt;
                    }
                    cc++;
                    cnts.put(c, cc);

                }
            }
            Set<Character> st = cnts.keySet();
            prnt(rows);
            int sz = st.size();
            Iterator<Character> it = st.iterator();
            System.out.println("------\nCounts\n");
            while(it.hasNext()){
                Character c = it.next();
                System.out.println(c +  " " + cnts.get(c));
            }


    }

    static void prnt(String[][] rows){

        for(int i =0; i < rows.length; i++){
                for(int j =0; j < rows[i].length; j++){
                    System.out.print(rows[i][j]);
                }
                System.out.println();
            }
    }
}

输出

制作

糊涂

小甜饼

计数

1

1

3

1

C 2

米3

氮2

O 4

是 4

K 5

于 2013-06-02T19:20:02.083 回答
0

一种从二维数组中对每个字符进行排序的方法。将看到通过更改 ArrayDatum 的数据成员以及如何实现 Cmpr,您可以以不同的方式对其他对象进行排序,因此可以有 Cmpr2、Cmpr3 ....

public class ArrayDatum <T>{
    private int locX; //probably do not need this -> original location but keot it for future use
    private int locY;
    private T data;

    public ArrayDatum(T dat, int x, int y){
        data = dat;
        locX = x;
        locY = y;
    }

    public int getLocationX(){
        return locX;
    }

    public void setLocationX(int x){
        locX = x;
    }

    public int getLocationY(){
        return locY;
    }

    public void setLocationY(int y){
        locY = y;
    }

    public T getData(){
        return data;
    }

    public void setData(T d){
        data = d;
    }

}

//比较器

import java.util.Comparator;

/**
* Comparator for ArrayDatum<String>
* Else not predictable
*/
public class Cmpr  implements Comparator{

    public int compare(Object  c1, Object b1){

        ArrayDatum c = (ArrayDatum )c1;
        ArrayDatum b = (ArrayDatum )b1;
        if(c == null || b == null) return 0;
        return c.getData().toString().compareTo(b.getData().toString());
    }
}    

//测试应用

import java.util.*;
public class Tst  {
    public static void main(String[] args){

        String[][] rows = {
            {"M","O","N","K","E","Y"},
            {"D","O","N","K","E","Y"},
            {"M","A","K","E"},
            {"M","U","C","K","Y"},
            {"C","O","O","K","Y"},
            };
            prnt(rows);
            List<ArrayDatum> dat = new ArrayList<ArrayDatum>();
            for(int i =0; i < rows.length; i++){
                for(int j =0; j < rows[i].length; j++){
                    ArrayDatum<String> dt = new ArrayDatum<String>(rows[i][j], i, j);
                    dat.add(dt);
                }
            }
            Cmpr cmpr = new Cmpr();
            Collections.sort(dat, cmpr);
            int sz = dat.size();
            System.out.println("------\nsort\n");
            for(int i =0; i < sz; i++){
                System.out.print(dat.get(i).getData() + " " );
            }
            //for(int i =0; i < sz; i++){
            //  ArrayDatum d = dat.get(i);
            //  rows[d.getLocationX()][d.getLocationY()] = d.getData().toString();
            //}
            int loc = 0;

            /*
            lay them into original array again
            for(int i =0; i < rows.length; i++){
                for(int j =0; j < rows[i].length; j++){
                    rows[i][j] = dat.get(loc).getData().toString();
                    loc++;
                }
            }


            prnt(rows);*/


    }

    static void prnt(String[][] rows){

        for(int i =0; i < rows.length; i++){
                for(int j =0; j < rows[i].length; j++){
                    System.out.print(rows[i][j]);
                }
                System.out.println();
            }
    }
}
于 2013-06-02T19:40:29.847 回答