Java 如何在表中使用if函数在循环中排列duplice字符串数组

Java 如何在表中使用if函数在循环中排列duplice字符串数组,java,arrays,loops,if-statement,Java,Arrays,Loops,If Statement,我想将一个随机输入字符串数组安排到一个表中,使用循环和if函数或其他更简单的方法 有最小的、小的、中等的、大的和最大的。并且字符串每个都是重复的x5 阵列是: String cow[][] = new String[5][5]; cow[0][0] = "big"; cow[0][1] = "smallest"; cow[0][2] = "small"; cow[0][3] = "medium"; cow[0][4] = "biggest";

我想将一个随机输入字符串数组安排到一个表中,使用循环和if函数或其他更简单的方法

有最小的、小的、中等的、大的和最大的。并且字符串每个都是重复的x5

阵列是:

    String cow[][] = new String[5][5];

    cow[0][0] = "big";
    cow[0][1] = "smallest";
    cow[0][2] = "small";
    cow[0][3] = "medium";
    cow[0][4] = "biggest";

    cow[1][0] = "smallest";
    cow[1][1] = "biggest";
    cow[1][2] = "medium";
    cow[1][3] = "small";
    cow[1][4] = "big";

    cow[2][0] = "medium";
    cow[2][1] = "biggest";
    cow[2][2] = "big";
    cow[2][3] = "smallest";
    cow[2][4] = "small";

    cow[3][0] = "small";
    cow[3][1] = "big";
    cow[3][2] = "smallest";
    cow[3][3] = "medium";
    cow[3][4] = "biggest";

    cow[4][0] = "biggest";
    cow[4][1] = "medium";
    cow[4][2] = "big";
    cow[4][3] = "small";
    cow[4][4] = "smallest";
我的阵列排列代码:

for (int j = 0; j < cow.length; j++) {
    for (int i = 0; i < cow[j].length; i++) {
        if (cow[i][j] == "smallest") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "small") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "medium") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "big") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "biggest") {
            System.out.print("| " + cow[i][j] + " |");
        }
    }
    System.out.println();
}

请教我,因为我对java很陌生。谢谢

您需要使用equals方法比较两个字符串

if ((cow[i][j]).equals("small")) {
........ }

在这里,我们使用ArrayList存储每一行,并使用Collections类对其进行排序,然后对每一行使用StringJoiner在值之间添加管道 您可以在代码注释中看到详细信息。 并记住导入所有必需的类

String cow[][] = new String[5][5];

    cow[0][0] = "big";
    cow[0][1] = "smallest";
    cow[0][2] = "small";
    cow[0][3] = "medium";
    cow[0][4] = "biggest";

    cow[1][0] = "smallest";
    cow[1][1] = "biggest";
    cow[1][2] = "medium";
    cow[1][3] = "small";
    cow[1][4] = "big";

    cow[2][0] = "medium";
    cow[2][1] = "biggest";
    cow[2][2] = "big";
    cow[2][3] = "smallest";
    cow[2][4] = "small";

    cow[3][0] = "small";
    cow[3][1] = "big";
    cow[3][2] = "smallest";
    cow[3][3] = "medium";
    cow[3][4] = "biggest";

    cow[4][0] = "biggest";
    cow[4][1] = "medium";
    cow[4][2] = "big";
    cow[4][3] = "small";
    cow[4][4] = "smallest";

    for (int i = 0; i < cow.length; i++) {
        List<String> list = new ArrayList<>(); //list for saving each row
        for (int j = 0; j < cow[i].length; j++) {

            list.add(cow[i][j]);

        }
        Collections.sort(list); // sort array
        Collections.reverse(list); // reverse array just for better order
        String big = list.get(4);  // since the biggest comes befor big
        list.set(4, list.get(3)); // we need to change them
        list.set(3, big);

        for(int x=0;x<list.size();x++){
if(x<list.size()-1){  System.out.print(list.get(x)+" | "); }
else { System.out.println(list.get(x)); } }

    } // end of outer loop
String-cow[][]=新字符串[5][5];
奶牛[0][0]=“大”;
奶牛[0][1]=“最小”;
奶牛[0][2]=“小”;
奶牛[0][3]=“中等”;
奶牛[0][4]=“最大”;
奶牛[1][0]=“最小”;
奶牛[1][1]=“最大”;
奶牛[1][2]=“中等”;
奶牛[1][3]=“小”;
奶牛[1][4]=“大”;
奶牛[2][0]=“中等”;
奶牛[2][1]=“最大”;
奶牛[2][2]=“大”;
奶牛[2][3]=“最小”;
奶牛[2][4]=“小”;
奶牛[3][0]=“小”;
奶牛[3][1]=“大”;
奶牛[3][2]=“最小”;
奶牛[3][3]=“中等”;
奶牛[3][4]=“最大”;
奶牛[4][0]=“最大”;
奶牛[4][1]=“中等”;
牛[4][2]=“大”;
奶牛[4][3]=“小”;
奶牛[4][4]=“最小”;
对于(int i=0;i对于(int x=0;x除主要问题外的可能重复:
如果(cow[i][j]=“最小”){
写入时的可能重复项,这将从侧面打印矩阵。可能的重复项是int,我无法理解,因为此示例仅使用常量,==将起作用,但您的点是有效的输出未更改:(这不是问题的答案,并且在评论中已经提供了使用
equals
的一般建议。应该删除此“答案”。您好,我发布了另一个答案以及此问题的确切解决方案,然后您可以在此处删除此帖子。我想我的netbeans版本不支持导入StringJoiner:(你确定吗?它是import java.util.StringJoiner;好的,那么让我用另一个代码替换StringJoiner部分。顺便说一句,如何转换它。)
String cow[][] = new String[5][5];

    cow[0][0] = "big";
    cow[0][1] = "smallest";
    cow[0][2] = "small";
    cow[0][3] = "medium";
    cow[0][4] = "biggest";

    cow[1][0] = "smallest";
    cow[1][1] = "biggest";
    cow[1][2] = "medium";
    cow[1][3] = "small";
    cow[1][4] = "big";

    cow[2][0] = "medium";
    cow[2][1] = "biggest";
    cow[2][2] = "big";
    cow[2][3] = "smallest";
    cow[2][4] = "small";

    cow[3][0] = "small";
    cow[3][1] = "big";
    cow[3][2] = "smallest";
    cow[3][3] = "medium";
    cow[3][4] = "biggest";

    cow[4][0] = "biggest";
    cow[4][1] = "medium";
    cow[4][2] = "big";
    cow[4][3] = "small";
    cow[4][4] = "smallest";

    for (int i = 0; i < cow.length; i++) {
        List<String> list = new ArrayList<>(); //list for saving each row
        for (int j = 0; j < cow[i].length; j++) {

            list.add(cow[i][j]);

        }
        Collections.sort(list); // sort array
        Collections.reverse(list); // reverse array just for better order
        String big = list.get(4);  // since the biggest comes befor big
        list.set(4, list.get(3)); // we need to change them
        list.set(3, big);

        for(int x=0;x<list.size();x++){
if(x<list.size()-1){  System.out.print(list.get(x)+" | "); }
else { System.out.println(list.get(x)); } }

    } // end of outer loop