2011-10-26 129 views
2

我有以下陣列添加值加倍[]數組列表

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}); 
} 
環路值

顯然這是每次添加一個新的數組與單個值...所以我怎麼得到它將所有添加到一個數組?

回答

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

有了類似的東西:

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); 
1

創建double[]第一,號碼添加到它,這陣列添加到List

(變量應該有可能被宣佈爲List,順便說一句,不是ArrayList,除非你專門將它傳遞的東西,明確預期的ArrayList

0
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