Java中的2d ArrayList添加数据 [英] 2d ArrayList in Java adding data

查看:101
本文介绍了Java中的2d ArrayList添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在家庭作业上几乎无需帮助。我必须创建一个10乘10 ArrayList ,而不是数组。这就是我所拥有的,我只需要提示如何进行for循环以将日期添加到2D ArrayList 。顺便说一下,这是用于放置等级的数据;从100到82.(是的我知道这是功课,但需要指向正确的方向)

I need little help on a homework assignment. I have to create a 10 by 10 ArrayList, not an array. This is what I have and I just need a hint on how to do a for loop to add the date to the 2D ArrayList. By the way this is for putting data that are grades; going from 100 to 82. (Yes I know it is homework but need to be pointed in the correct direction)

public void q6()
{
  //part a
  ArrayList<ArrayList<Double>> grades;
  //part b
  grades = new ArrayList<ArrayList<Double>>(10);
  //second dimension
  grades.add(new ArrayList<Double>(10));
  for(int i = 0; i < 10; i++)
  {
    for(int j = 0; j < 10; j++)
    {
      // grades.get().add(); Not sure what to do here?
      // If this was an array I would do something like:
      // grades[i][j] = 100 -j -i;
    }
  }
}


推荐答案

鉴于代码,您要做的就是稍微更改一下以接收10x10矩阵。

Given the code, all you left to do is change it a little to receive 10x10 matrix.

public class Main
{
   public static final int ROW_COUNT = 5;
   public static final int COL_COUNT = 10;

   public static void main(String[] args)
   {
      ArrayList<ArrayList<Double>> grades = new ArrayList<ArrayList<Double>>();

      for (int i = 0; i < ROW_COUNT; i++)
      {
         ArrayList<Double> row = new ArrayList<Double>();

         for (int j = 0; j < COL_COUNT; j++)
         {
            row.add(100.0 - j - i);
         }

         grades.add(row);
      }

      for (int i = 0; i < ROW_COUNT; i++)
      {
         for (int j = 0; j < COL_COUNT; j++)
         {
            System.out.print(grades.get(i).get(j));
            System.out.print(", ");
         }

         System.out.println("");
      }
   }
}

这篇关于Java中的2d ArrayList添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆