java - 在使用变量时尝试在 "for"循环中确定 array.length

标签 java arrays

我有一个名为“multiarray”的二维数组。第一个是 [7] ,第二个当前初始化为 [500]。我正在从一个文本文件中读取数据,该文件中有随机数量的条目进入第二个数组。该数组永远不会有 500 个条目,我需要知道有多少个条目。

我在想y < multiarray[x].length会告诉我我需要知道什么,但它似乎在循环。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;

public class DOW 
{
    public static void main(String[] args) 
    {
        File file = new File ("/Users/***/RandomInts.txt") ;
        try
        {
        Scanner scanner = new Scanner(file) ;
        //Formatter formatter = new Formatter (new File ("outputfile.txt")) ;

        int [][] multiarray = new int [7][500];

        int counter1 = 0; 
        int counter2 = 0; 
        int counter3 = 0; 
        int counter4 = 0;
        int counter5 = 0;
        int counter6 = 0; 
        int counter7 = 0;

          while (scanner.hasNext())
            {
                int dow = scanner.nextInt () ;
                int temp = scanner.nextInt () ;
                dow = dow -1;

                if(dow == 0)
                {
                    multiarray[dow][counter1] = temp;
                    counter1 ++;
                }

                if(dow == 1)
                {
                    multiarray[dow][counter2] = temp;
                    counter2 ++;
                }

                if(dow == 2)
                {
                    multiarray[dow][counter3] = temp;
                    counter3 ++;
                }

                if(dow == 3)
                {
                    multiarray[dow][counter4] = temp;
                    counter4 ++;
                }

                if(dow == 4)
                {
                    multiarray[dow][counter5] = temp;
                    counter5 ++;
                }

                if(dow == 5)
                {
                    multiarray[dow][counter6] = temp;
                    counter6 ++;
                }

                if(dow == 6)
                {
                    multiarray[dow][counter7] = temp;
                    counter7 ++;
                }
            }

            for (int x = 0; x < 7; x++)
                {
                    int hightemp = 0;
                    int lowtemp = 0;
                    int avetemp = 0;

                    for (int y = 0; y < multiarray[x].length ; y++)
                    {
                        if (multiarray[x][y] > hightemp)
                        {
                            hightemp = multiarray[x][y];
                        }

                        if (multiarray[x][y] < lowtemp)
                        {
                            lowtemp = multiarray[x][y];
                        }

                        avetemp = avetemp + multiarray[x][y];
                    }
                    //avetemp = avetemp /    
                }

            //formatter.format (" %d %d \n " , dow , temp ) ;

            //formatter.flush () ;
            //formatter.close () ;

            //int[] dow;
            //dow = new int [7];

            //int[] temp;
            //temp = new int [100];

            //int x = 0;
            //while ( x < temp.length)
            //{
                //System.out.println (temp[x]);
                //++x;
                //temp[x]=0;
            //}
    }         

        catch (FileNotFoundException e)
        {} 
     }
}

我想知道数组长度的原因是因为我想调用这个数字进行一些数学运算。

avetemp = avetemp / multiarray[x].length

我已经有一个 [x] 的计数器,因为它是从文件中读入的,但我希望不要在这里使用它,这样我就不必手动写出所有内容。

输入文本文件的样例:

5 67
2 -15 
1 -40 
7 32 
6 -24 
7 33 
5 -32 
3 57 
3 41 
6 51 

基本上第一列是星期几,第二列是温度。

最佳答案

现在我知道您的输入是什么样的了。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class DOW {

  public static void main(String[] args) {
    File file = new File("/Users/***/RandomInts.txt");
    try {
      Scanner scanner = new Scanner(file);

      ArrayList<Integer>[] multiarray = new ArrayList[7]; // This is how one would make an array of ArrayList's.
      for (int i = 0; i < multiarray.length; i++)
        multiarray[i] = new ArrayList<Integer>();

      while (scanner.hasNextInt()) { //This is how you would save the values. You were saving temperatures incorrectly.
        int dow = scanner.nextInt() - 1; //Get day of week.
        int temp = scanner.nextInt(); //Get temperature. [Would throw exception here if your input was bad]
        multiarray[dow].add(temp); //Store temperature in the array representing the day of week.
      }

      // Looks like you want to find min, max and average in each column here.
      for (int x = 0; x < 7; x++) {
        int hightemp = Integer.MIN_VALUE; // Set this to something really low. (You will see why in a minute)
        int lowtemp = Integer.MAX_VALUE; // Set this to something really high.
        double avetemp = 0; // You seem to be using ave as "sum" right now and then you plan on dividing which is fine. This should also be a double.
        int size = multiarray[x].size(); // No point calling .size() [or in general any function] over and over again. Its better to just cache the answer.
        for (int y = 0; y < size; y++) {
          int num = multiarray[x].get(y); // Same logic as size here.
          hightemp = Math.max(hightemp, num);
          lowtemp = Math.min(lowtemp, num);
          avetemp += num;
        }
        avetemp = avetemp / size;
        // P.S.: Also you probably want to save the values of hightemp, lowtemp, avetemp somewhere after doing all this work!
        // Removed the rest of your code as it is irrelevant for your question.
      }
      // Close your streams.
      scanner.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (Exception e) {
      // This would happen if your input file is bad.
      e.printStackTrace();
    }
  }
}

编辑:您正在正确添加内容。我的错。

关于java - 在使用变量时尝试在 "for"循环中确定 array.length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158469/

相关文章:

java - 如何在 olingo v4.3 中实现实体绑定(bind) odata 操作

java - Minecraft 在 Windows 上如何具有 .exe 扩展名

java - H2内存数据库初始化数据

PHP 打印使用 While 循环和数组返回的所有值

java - 将文本文件写入数组,然后在 Java 中返回它

java - Java如何实现字符串池?

java - Android 文本验证

c - 如何在 C 中漂亮地打印数组

c - 使用模运算符 c 遍历数组

c - 八进制转十进制的c程序