简体   繁体   中英

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. I have a String arrray[25][53]. How do I make array[0-25][1-52] an integer? I assuming with .parseInt but Im not really sure how they work considering I am still learning 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. 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<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. Othe than arrays, List s usually have no predefined size limitation.

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:

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[] 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.

(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)

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.

Eg

Integer.toString(42);

However, this will cause pain when trying to perform Math operations on them later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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