java替换数组的元素

问题描述 投票:0回答:1

主要问题将在底部。在下面的文本文件中,假设第一个整数是 a,第二个整数是 b,第三个整数是 c,依此类推。该程序接受 a、b 和 c,解析它们,将它们放入 myCalculations 方法中,该方法返回一个包含两个整数的字符串。解析字符串,将 a 和 b 替换为所述返回字符串中的整数,然后循环的下一次迭代将采用 a 和 b 以及整数 d 的新值。这将一直持续到最后将 a 和 b 打印给用户。

两个文本文件的输入如下:

文本文件格式如下:

200 345 
36
45
36
21

这是从文件中读取的内容,它按预期工作,我将其放在这里作为上下文。 tl;dr is results[] 是第一行的整数数组。 (整数a和b)

public class conflictTrial
{
    BufferedReader in;
    public static void conflictTrial() throws FileNotFoundException 
    {
        System.out.print('\u000c');
        System.out.println("please enter the name of the text file you wish you import. Choose either costs.txt or lotsacosts.txt Nothing else");
        Scanner keyboard = new Scanner(System.in);
        String filename = keyboard.nextLine();
        File file = new File(filename);
        BufferedReader in = new BufferedReader(new FileReader(file));

        String element1 = null;
        try {
            element1 = in.readLine();
        }catch (Exception e) {
            //  handle exception
        }

        String[] firstLine = element1.split(" ");

        Arrays.stream(firstLine).forEach(fl -> {
            //System.out.println("First line element: \t\t\t" + fl);
        });

        int[] results = new int[100];
        for (int i = 0; i < firstLine.length; i++) 
        {
            try {
                int stuff = Integer.parseInt(firstLine[i]);
                results[i] = stuff;
            }
            catch (NumberFormatException nfe) {
                // handle error   
            }
        }     

bufferreader读入文件,for循环将整数解析为数组results[]。接下来,解析剩余的行并调用方法 myCalculations:

 String otherElement = null;
 int[] aliveSoldiers = new int[100];
 int [] things = new int [100];
 int[] newResults = new int[100];
 try {
      while ((otherElement = in.readLine()) != null) {       // main loop
          System.out.println("Line to process:\t\t\t" + otherElement);
          String[] arr = otherElement.split(" ");

          for (int k = 0; k <arr.length; k++) 
          {
              int thingsResult = Integer.parseInt(arr[k]);
              things[k] = thingsResult;
              System.out.println("number of days: \t\t\t"+things[k]);

              aliveSoldiers[0] = results[0];
              aliveSoldiers[1] = results[1];
              String returnAliveSoliders = myCalculations(aliveSoldiers[0], aliveSoldiers[1], things[k]);
              System.out.println("return soldiers alive: \t\t"+returnAliveSoliders);

              String[] newItems = returnAliveSoliders.split(" ");


              for (int f = 0; f < newItems.length; f++) 
              {
                  int newParse = Integer.parseInt(newItems[f]);
                  newResults[f] = newParse;
                  aliveSoldiers[0] = newResults[0];
                  aliveSoldiers[1] = newResults[1];
              } 
              k++;
          }
       } 
   }
    catch (Exception e) {
       e.printStackTrace();
   }
}

当前代码执行以下操作:主循环迭代的第一次采用整数 a、b 和 c,第二次迭代采用相同的整数 a 和 b(200 和 345,初始值)和整数 d,第三次迭代采用相同的整数 a 和 b(200 和 345,初始值)和 a 和 b 的值均为整数 e。我尝试使用以下代码解决此问题:

aliveSoldiers[0] = newResults[0];
aliveSoldiers[1] = newResults[1];

我需要从 myCalculations 方法中获取整数(在 k-modifier 循环中解析),并将它们覆盖到 aliveSoldiers[0] 和 aliveSoldiers [1] 中,以便程序读取下一行,获取新整数,然后继续,直到剩下的日子已经不多了。

java arrays loops
1个回答
0
投票

老实说,我还不明白整个练习应该做什么,但是由于您使用的索引,您启发的代码可能是错误的:这些索引始终为 0 和 1,即使通过其他索引执行循环也是如此。在第二个代码片段的末尾,f-for 在递增的索引“f”处修改数组“newResults”,但该数组始终在相同的索引处读取:0 然后是 1。因此,如果“f”得到高于“1”的值则“aliveSoldiers”的元素保持不变。

特别是aliveSoldiers只修改了前两个索引,其他索引根本不使用。

您需要类似堆栈或类似队列的行为吗?

© www.soinside.com 2019 - 2024. All rights reserved.