【问题标题】:How to store a integer into a string multi-dimensional array in java如何在java中将整数存储到字符串多维数组中
【发布时间】:2014-10-30 12:28:54
【问题描述】:

我需要存储 25 个不同的数组,其中位置 1 是人名,其余索引为 int 来进行数学运算。我有一个字符串数组[25][53]。如何使 array[0-25][1-52] 成为整数?我假设使用 .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);

编辑: 我会使用 OOP 或地图,但考虑到我们在课程中还没有走那么远,我不想越界让我的教授生气。我知道这不是最直观的,但这是我最终想出的任何机构看到的问题?

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;        

            }

【问题讨论】:

  • 还没有到 OOP。我相信我会在接下来的两周内这样做。
  • 使用地图查看答案

标签: java loops multidimensional-array arrays


【解决方案1】:

您需要为此用例使用 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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您应该遵循面向对象的方法,并将您的志愿者数据封装在这样的类中:

      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;
          }
      
          .
          .
          .
      
      }
      

      现在您可以将它们存储在List 或数组中。除了数组,Lists 通常没有预定义的大小限制。

      List<Volunteer> volunteers = new ArrayList<>();
      
      // Or as an array
      
      Volunteer[] volunteers = new Volunteer[25];
      

      然后您可以遍历列表并使用 getter 和 setter 进行数学运算,如下所示:

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

      【讨论】:

        【解决方案4】:

        首先,我认为你需要一个字符串数组,然后是长度为 52 的多维整数数组。Java 是一种非常静态的语言,它不会让你做一些疯狂的事情,比如存储一个字符串数组中的整数。

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

        其次,我认为您真的可以在这里使用一些不错的面向对象,为 Volunteer 创建一个类,并在其中存储一个字符串,其中包含名称和这些时间的 int 数组。

        (附注:稍后尝试搜索 getter 和 setter。通常的做法是声明具有私有范围的类变量并定义访问它们的方法,但我在这里将它们用于公共范围只是为了保持示例简短)

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

        第三,如果你真的想像现在这样做,我认为你应该使用地图。

        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
        

        【讨论】:

          【解决方案5】:

          如果您坚持使用 String 数组,则需要使用 Integer 类的 toString 函数。

          例如

          Integer.toString(42);
          

          但是,这会在稍后尝试对其执行数学运算时造成痛苦。

          【讨论】:

          • 我需要每个数组的第一个索引( array[0][0],array[1][0]...etc)作为名称,因此我将其声明为字符串数组。
          • 好吧,我错过了。不过,您仍然想使用整数数组。将一堆您希望对其进行数学运算的整数放入字符串数组中会导致痛苦。所以使用其他答案中建议的地图可能是这里的方法。
          猜你喜欢
          • 1970-01-01
          • 2020-08-13
          • 2015-02-08
          • 1970-01-01
          • 2019-08-10
          • 1970-01-01
          • 1970-01-01
          • 2017-09-06
          • 1970-01-01
          相关资源
          最近更新 更多