2016-10-12 6 views
-4

私はこの配列arraylistに配列を追加して結果を得るには?

double[] priceInDouble={89.35, 55.95, 95.90, 60.00, 116.55, 75.10, 47.00, 18.05}; 

からaddedPrices ArrayListに要素を追加し、次のコードと同じのArrayListの合計が、運を取得しようとしています。

ArrayList<Double> addedPrices = new ArrayList<Double>(); 
       for(int i = 0; i < 1 ; i++) { 
        addedPrices.add(i,priceInDouble[position]); 
        double price = 0; 
        for (double d : addedPrices){ 
         price +=d; 
         TextView textView = (TextView)activity.findViewById(R.id.counter_text); 
         textView.setText(NumberFormat.getInstance().format(price)); 
         textView.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED); 
        } 
       } 
+0

投稿する前にスタックオーバーフローを検索してください。 –

答えて

1

あなたのロジックは少しオフになっている、のは、それを修正してみましょう助けが必要です:あなたは、配列を移動し終わったら

ArrayList<Double> addedPrices = new ArrayList<>(priceInDouble.length); 
double sum = 0; 

for(i = 0; i < priceInDouble.length; i++){ 

    addedPrices.add(priceInDouble[i]); 
    sum += priceInDouble[i]; 
} 

/*I'm assuming you actually needed 
the ArrayList and were not doing it just to add the 
values with a for-each loop.. */ 

だから今、あなたはすでに合計を持っています。

とにかくコードが非常に壊れていますので、次回はもっと重視し、投稿前に他の回答を探してください。欠落していたものはオンラインで簡単に見つけることができる基本的なロジック/構文です。

関連する問題