简体   繁体   English

如何在Java中将整数存储到字符串多维数组中

[英]How to store a integer into a string multi-dimensional array in java

I need to store 25 different arrays with position one being the name of the person and the rest of the indexs as a int to do math operations. 我需要存储25个不同的数组,其中第一个位置是人的名字,其余的索引作为做数学运算的整数。 I have a String arrray[25][53]. 我有一个String arrray [25] [53]。 How do I make array[0-25][1-52] an integer? 如何使array [0-25] [1-52]为整数? I assuming with .parseInt but Im not really sure how they work considering I am still learning java. 我假设使用.parseInt,但是考虑到我仍在学习Java,我不太确定它们如何工作。

String[][] volunteerNamesAndHours = new String [25][53];
    int ID = 0;
    int week;
    do{
        volunteerNamesAndHours[ID][NAME] = input.next();
            for(week = 1; week < 53; week++){
                 volunteerNamesAndHours[ID][week] = Integer.parseInt(null, ID);

EDIT: I would use OOP or a map but considering we havent got that far in the course I don't want to over step my boundaries and making my professors mad. 编辑:我会使用OOP或地图,但考虑到我们还没有走得那么远,我不想超越自己的界限,让我的教授发疯。 I know it is not the most intuitive but this what I ended up coming up with any body see a problem? 我知道这不是最直观的方法,但是我最终想到的这个问题是什么?

public static String[][] getvolunteerChart(Scanner input){
    String[][] volunteerNamesAndHours = new String [25][53];
    int ID = 0;
    int week;
    do{
        volunteerNamesAndHours[ID][NAME] = input.next();
            for(week = 1; week < 53; week++){
                 volunteerNamesAndHours[ID][week] = Integer.toString(input.nextInt());
            }
        ID++;    
    }
    while(ID <= 24);

    return volunteerNamesAndHours;        

            }

我建议使用哈希图将名称存储为键,将整数数组列表存储为值,例如

Map<String, ArrayList<Integer>> volunteerNamesAndHoursMap = new HashMap<String, ArrayList<Integer>>();

You need to use a Map structure for this use-case. 您需要针对此用例使用Map结构。

Map<String, Integer[]> personIDs = new HashMap<>(25);

personIDs.put("Peter", new Integer[]{5,22,7734});

personIDs.get("Peter");//returns the array 5,22,7734

You should follow an object-oriented approach and encapsulate your volunteers data in a class like this: 您应该遵循一种面向对象的方法,并将您的志愿者数据封装在这样的类中:

public class Volunteer {

    private int id;

    private String name;

    private int hours;

    .
    .
    .

    public in getId() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    .
    .
    .

}

Now you are able to store them in a List or an array. 现在,您可以将它们存储在List或数组中。 Othe than arrays, List s usually have no predefined size limitation. 除了数组之外, List通常没有预定义的大小限制。

List<Volunteer> volunteers = new ArrayList<>();

// Or as an array

Volunteer[] volunteers = new Volunteer[25];

You can then iterate over the list and do your math using the getters and setters like this: 然后,您可以遍历列表,并使用如下的getter和setter进行数学运算:

for (Volunteer volunteer : volunteers) {
    int oldHours = volunteer.getHours();
    int newHours = oldHours + 8;

    volunteer.setHours(newHours);
}

First of all, I think you'd need an array of Strings and then the multi-dimensional array of ints with length 52. Java is a very static language, and it won't let you do crazy things like storing an Integer in a String array. 首先,我认为您需要一个String数组,然后是一个长度为52的int多维数组。Java是一种非常静态的语言,它不会让您做疯狂的事情,例如将Integer存储在字符串数组。

String[] volunteerNames = new String [25];
int[] volunteerHours = new int [25][52];

Second of all, I think you could really use some nice Object orientation here, making one class for Volunteer and storing in it a string with the name and an array of int's for those hours. 第二,我认为您确实可以在这里使用一些不错的Object定向,为Volunteer创建一个类,并在其中存储一个带有名称和int数组的字符串。

(PS on that: Try searching about getters and setters later. It's common practice to declare class variables with private scope and define methods to access them, but I used them with public scope here just to keep the example short) (关于此的PS:稍后尝试搜索getter和setter。通常的做法是声明具有私有作用域的类变量并定义访问它们的方法,但是我在这里将它们与公共作用域一起使用只是为了使示例简短

public class Volunteer {

    public String name;
    public int[] hours;

    Volunteer(String name, int[] hours) {
        this.name = name;
        this.hours = hours;
    }
}

Third of all, if you really want to do it like you're doing, I think you should probably use a Map. 第三,如果您真的想像做的那样做,我想您应该使用地图。

Map<String, Integer[]> volunteersHours = new HashMap<String, Integer[]>();
volunteersHours.put("John", new Integer[] {1, 2, 3, 4, etc...});

volunteersHours.get("John"); // will return the hours array

If you insist on using a String array you will need to use the toString function of the Integer class. 如果您坚持使用String数组,则需要使用Integer类的toString函数。

Eg 例如

Integer.toString(42);

However, this will cause pain when trying to perform Math operations on them later. 但是,这将在以后尝试对它们执行数学运算时引起痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM