Java 在二维数组中输入和存储字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18884209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:53:36  来源:igfitidea点击:

Input and storing Strings in two-dimensional arrays

javaarrayssorting2d

提问by Sal

My teacher explained two dimensional arrays in literally two paragraphs. He didn't give me any information on how to create them besides that and now I have to do an assignment.

我的老师用两段文字解释了二维数组。除此之外,他没有给我任何关于如何创建它们的信息,现在我必须做一个作业。

I've read up a lot about it and I somewhat understand how a 2D array is like an array of arrays, but I'm still completely and utterly confused about how to apply it.

我已经阅读了很多关于它的内容,并且我有点理解二维数组与数组数组的相似之处,但我仍然对如何应用它感到完全困惑。

The assignment itself is very simple. It asks me to create a program that will ask a user for ten Criminal Records, (name, crime, year). This program will store the records in a two-dimensional array and then sort them using the selection sort.

任务本身非常简单。它要求我创建一个程序,该程序将向用户询问十份犯罪记录(姓名、犯罪记录、年份)。该程序将记录存储在二维数组中,然后使用选择排序对它们进行排序。

I know this is probably wrong, but here is what I have so far based on what I've read:

我知道这可能是错误的,但根据我所阅读的内容,这是迄今为止我所拥有的:

    public static void main(String[] args)throws IOException {

    //create array
    String[][] Criminals = new String[10][3]; // create 3 columns, 10 rows

    int i, j;
    int smallest; //smallest is the current smallest element
    int temp; //make an element swap
    String line;

    //loop to request to fill array   
    for (int row = 1; row < Criminals.length; row++){
        for (int col = 1; col < Criminals[row].length; col++){
            System.out.print("Enter a criminal name: ");
            Criminals[row][col] = br.readLine();

        }
    }
}

So far, I'm just trying to get the input and store it.

到目前为止,我只是想获取输入并存储它。

(Please try to be patient and thorough with me! Coding isn't my strongest point, but I'm trying to learn.) Any help would be amazing! Thanks in advance. :)

(请尽量对我保持耐心和彻底!编码不是我的强项,但我正在努力学习。)任何帮助都会很棒!提前致谢。:)

采纳答案by Zong

It looks fine for the most part. You should index arrays starting from 0, not 1. Your current code works but I'm guessing you don't want the same prompt for all entries. Thus it may be a good idea to use a single loop instead:

大多数情况下它看起来不错。您应该从 0 开始索引数组,而不是 1。您当前的代码有效,但我猜您不希望所有条目都具有相同的提示。因此,改用单个循环可能是个好主意:

for (int row = 0; row < Criminals.length; row++) {
    System.out.print("Enter a criminal name: ");
    Criminals[row][0] = br.readLine();

    System.out.print("Enter a crime: ");
    Criminals[row][1] = br.readLine();

    System.out.print("Enter a year: ");
    Criminals[row][2] = br.readLine();
}

回答by Marco Altran

import java.util.Scanner;


public class Main {


    public static void main(String[] args) {



        Scanner in = new Scanner(System.in);

             //create array
            String[][] criminals = new String[10][3]; // create 3 columns, 10 rows

            int i, j;
            int smallest; //smallest is the current smallest element
            int temp; //make an element swap
            String line;

            //loop to request to fill array   
            for (int row = 0; row < criminals.length; row++){
                    System.out.print("Enter a criminal name: ");
                    while(in.hasNext()){
                    criminals[row][0] = in.nextLine();
                    System.out.print("Enter a crime: ");
                    criminals[row][1] = in.nextLine();
                    System.out.print("Enter a year: ");
                    criminals[row][2] = in.nextLine();
                    }


            }

    }


}

This will print the commands you need from user and will store it in criminals. You may sort in the end. Since you didn't gave any information how you want it sorted, I will leave it for you to do it.

这将打印用户需要的命令并将其存储在犯罪分子中。最后可以排序。由于您没有提供任何您希望如何排序的信息,我将把它留给您去做。

PS: I changed the 2d array name from Criminals to criminals, it's a java's good practice to not use capital words for attributes and variables (use it only for class names)

PS:我将 2d 数组名称从犯罪分子更改为犯罪分子,java 的好习惯是不要对属性和变量使用大写单词(仅用于类名)