将值添加到 double[] 数组列表

发布于 2024-12-12 04:40:27 字数 411 浏览 0 评论 0原文

我有以下数组

ArrayList<double[]> db_results = new ArrayList<double[]>(); 

,我想添加这样的值

db_results.add(new double[] {0,1,2});

,但在这样的循环中

for ( int i = 0 ; i <= 2; i++) {
    double val = Double.parseDouble(i);
    db_results.add(new double[] {val});
}

,显然每次都会使用单个值添加一个新数组......那么我如何才能将其全部添加到一个数组中呢?

I have the following array

ArrayList<double[]> db_results = new ArrayList<double[]>(); 

and I would like to add values like this

db_results.add(new double[] {0,1,2});

but in a loop like this

for ( int i = 0 ; i <= 2; i++) {
    double val = Double.parseDouble(i);
    db_results.add(new double[] {val});
}

obviously this is adding a new array each time with the single value... so how do I get it to add all into one array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后eg是否自 2024-12-19 04:40:27
ArrayList<double[]> db_results = new ArrayList<double[]>();

double[] nums = new double[3];
for (int i = 0; i < nums.length; i++) {
  nums[i] = i;
}
db_results.add(nums);
ArrayList<double[]> db_results = new ArrayList<double[]>();

double[] nums = new double[3];
for (int i = 0; i < nums.length; i++) {
  nums[i] = i;
}
db_results.add(nums);
执手闯天涯 2024-12-19 04:40:27

首先创建 double[],向其中添加数字,然后将该数组添加到 List 中。

(该变量可能应该声明为 List,顺便说一句,而不是 ArrayList,除非您专门将其传递给明确需要 ArrayList 的对象代码>.)

Create the double[] first, add the numbers to it, and add that array to the List.

(The variable should likely be declared as a List, btw, not an ArrayList, unless you're specifically passing it to something that explicitly expects an ArrayList.)

草莓酥 2024-12-19 04:40:27

有了类似的东西:

max = 3;
double[] doubles = new double[max];
for ( int i = 0 ; i < max; ++i)
    {
     double val = Double.parseDouble(i);
     doubles[i] = val;
    }
db_results.add(doubles);

With something like that :

max = 3;
double[] doubles = new double[max];
for ( int i = 0 ; i < max; ++i)
    {
     double val = Double.parseDouble(i);
     doubles[i] = val;
    }
db_results.add(doubles);
人生戏 2024-12-19 04:40:27
import java.util.Scanner;
class DarrayEx2
{
    public static void main(String args[])
      {
       int a[][]=new int[3][3];
       int r,c,sumr;
       Scanner s=new Scanner(System.in);
        for(r=0;r<a.length;r++)
         {
           for (c=0;c<a.length ;c++ )
            {
             System.out.println("enter an  element");
             a[r][c]=s.nextInt();
            }
          }
          for(r=0;r<a.length;r++)
            {
              sumr=0;
              System.out.println("elements in a["+r+"] row is");
               for (c=0;c<a[1].length ;c++ )
                 {
                   System.out.println(" "+a[r][c]);
                   sumr = sumr+a[r][c];
                 }

               System.out.println(" = "+sumr);
               System.out.println("  ");
             }
         }
}

来源:http://www.exceptionhandle.com/portal /java/core-java/part-12-arrays.htm

import java.util.Scanner;
class DarrayEx2
{
    public static void main(String args[])
      {
       int a[][]=new int[3][3];
       int r,c,sumr;
       Scanner s=new Scanner(System.in);
        for(r=0;r<a.length;r++)
         {
           for (c=0;c<a.length ;c++ )
            {
             System.out.println("enter an  element");
             a[r][c]=s.nextInt();
            }
          }
          for(r=0;r<a.length;r++)
            {
              sumr=0;
              System.out.println("elements in a["+r+"] row is");
               for (c=0;c<a[1].length ;c++ )
                 {
                   System.out.println(" "+a[r][c]);
                   sumr = sumr+a[r][c];
                 }

               System.out.println(" = "+sumr);
               System.out.println("  ");
             }
         }
}

source : http://www.exceptionhandle.com/portal/java/core-java/part-12-arrays.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文