我有多个数组存储在TreeMap中,然后我想检索这些数组并将它们乘以数字。之后,我想在一个新数组中对每个列值进行求和。
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
public class readingLargeFile {
static String[] words;
static Map<String, double[]> m1 = new TreeMap();
static Map<String,Double> m2 = new TreeMap();
public static void main(String args[]){
//First TreeMap
double count[]={3.9, 1.2, 6.2, 1.8, 7.6, 3.8};
double count1[]={1.6, 7.2, 6.2, 2.3, 1.8, 0.0};
double count2[]={1.6, 5.5, 1.8, 8.8, 0.0, 0.0};
double count3[]={0.0, 0.0, 0.0, 2.3, 0.0, 0.0};
double count4[]={2.0, 2.2, 1.2, 3.9, 2.3, 4.4};
double count5[]={3.4, 0.0, 1.9, 2.4, 0.5, 2.0};
m1.put("apple",count);
m1.put("orange",count1);
m1.put("banana",count2);
m1.put("cherry",count3);
m1.put("lemon",count4);
m1.put("strawbarry",count5);
//for(Map.Entry<String, double []> e : m1.entrySet()) {//print First TreeMap content
//System.out.println(e.getKey()+":"+Arrays.toString(e.getValue()));
//}
//second TreeMap
m2.put("apple", 2.1);
m2.put("cherry", 1.9);
m2.put("grasb", 3.0);
m2.put("strawbarry", 4.1);
double[] finalSum = new double[6];
double first=0;
double second=0;
double third=0;
double fourth=0;
double fifth=0;
double sixth=0;
String key="";
for ( Map.Entry<String,Double> entry : m2.entrySet() ) {//for loop through the second TreeMap
if ( m1.containsKey(entry.getKey()) ) {//check if the first TreeMapp contain same key from second TreeMap
//if the key is common in m1 and m2, multiply the values
key=entry.getKey();
double y=entry.getValue();//the number from second TreeMap
double w[]=m1.get(entry.getKey());//the array from first TreeMap
//for loop to multiple the array from first TreeMap by the number in second TreeMap for same key
/*for example if the key=apple
then
{3.9*2.1, 1.2*2.1, 6.2*2.1, 1.8*2.1, 7.6*2.1, 3.8*2.1}
*/
for (int u=0;u<w.length;u++){
finalSum[u]= w[u]*y;}
System.out.println(key+" "+Arrays.toString(finalSum));
}}
for (int u=0,t=0;u<finalSum.length;u++,t++){
first+=finalSum[t];
second+=finalSum[t];
third+=finalSum[t];
fourth+=finalSum[t];
fifth+=finalSum[t];
sixth+=finalSum[t];
}
System.out.println(first+"\n"+second+"\n"+third+"\n"+fourth+"\n"+fifth+"\n"+sixth);}}
然后我的输出:
apple [8.19, 2.52, 13.020000000000001, 3.7800000000000002, 15.959999999999999, 7.9799999999999995]
cherry [0.0, 0.0, 0.0, 4.369999999999999, 0.0, 0.0]
strawbarry [13.939999999999998, 0.0, 7.789999999999999, 9.839999999999998, 2.05, 8.2]
41.81999999999999
41.81999999999999
41.81999999999999
41.81999999999999
41.81999999999999
41.81999999999999
任何人都知道问题是什么。:(
答案 0 :(得分:0)
我猜你应该定义int t = 0;在循环之前。不确定
答案 1 :(得分:0)
最终编辑 在重构问题之后,我对以前提出的解决方案进行了更改,但仍然使用相同的方法,请参阅下面的“原始答案”了解详情
以下是该问题的最终修正代码,解决了之前所述的问题。
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
public class ReadingLargeFile {
static String[] words;
static Map<String, double[]> m1 = new TreeMap();
static Map<String, Double> m2 = new TreeMap();
public static void main(String args[]) {
//First TreeMap
double count[] = {3.9, 1.2, 6.2, 1.8, 7.6, 3.8};
double count1[] = {1.6, 7.2, 6.2, 2.3, 1.8, 0.0};
double count2[] = {1.6, 5.5, 1.8, 8.8, 0.0, 0.0};
double count3[] = {0.0, 0.0, 0.0, 2.3, 0.0, 0.0};
double count4[] = {2.0, 2.2, 1.2, 3.9, 2.3, 4.4};
double count5[] = {3.4, 0.0, 1.9, 2.4, 0.5, 2.0};
m1.put("apple", count);
m1.put("orange", count1);
m1.put("banana", count2);
m1.put("cherry", count3);
m1.put("lemon", count4);
m1.put("strawbarry", count5);
//second TreeMap
m2.put("apple", 2.1);
m2.put("cherry", 1.9);
m2.put("grasb", 3.0);
m2.put("strawbarry", 4.1);
double[][] addedresults = getFinalSums(m1, m2);
double[] finalResults = getSummedColumns(addedresults);
System.out.println("added value arrays: " + Arrays.deepToString(addedresults));
System.out.print("final summed array: " + Arrays.toString(finalResults));
}
public static double[] getSummedColumns(double[][] array) {
double[] results = new double[array.length];
for (int i = 0; i < results.length; i++) {
for (int j = 0; j < array.length; j++) {
results[i] += array[j][i];
}
}
return results;
}
public static double[][] getFinalSums(Map<String, double[]> m1, Map<String, Double> m2) {
int sharedSums = 0;
for (Map.Entry<String, Double> entry : m2.entrySet())
if (m1.containsKey(entry.getKey()))
sharedSums++;
double[][] finalSum = new double[sharedSums][];
int i = 0;
for (Map.Entry<String, Double> entry : m2.entrySet()) {//for loop through the second TreeMap
if (m1.containsKey(entry.getKey())) {//check if the first TreeMapp contain same key from second TreeMap
//if the key is common in m1 and m2, multiply the values
double y = entry.getValue();//the number from second TreeMap
double w[] = m1.get(entry.getKey());//the array from first TreeMap
finalSum[i] = new double[w.length]; // instantiate the new array in the 2d array
for (int j = 0; j < w.length; j++) {
finalSum[i][j] = w[j] * y; // add in the values in the correct spot in the 2d array
}
i++; // increase the current spot in the 2d array
}
}
return finalSum;
}
}
以下是此类产生的输出
增值数组:[[8.19,2.52,13.020000000000001,3.7800000000000002,15.959999999999999,7.9799999999999995],[0.0,0.0,0.0,4.369999999999999,0.0,0.0],[13.939999999999998,0.0,7.789999999999999,9.83999999999999998,2.05,8.2]]
最终求和阵列:[22.129999999999995,2.52,20.810000000000002]
编辑原始答案
for (int u=0;u<w.length;u++){
finalSum[u]= w[u]*number;//multibly the array with the number
System.out.println(Arrays.toString(finalSum));}//print the array
}
注意你是如何每次都写出结果,但是在下一次循环运行时,你要覆盖结果,这就是你需要二维数组的地方,如
double[][] finalSum = new int[w.length][];
for(int i = 0; i < w.length; i++){
for (int u=0;u<w.length;u++){
finalSum[i][u]= w[u]*number;//multibly the array with the number
System.out.println(Arrays.toString(finalSum));}//print the array
}
}
现在应该在2d数组中保存数组的每一行,然后可以按照下面的描述对其进行接收和求和
已编辑 现在按要求添加列,仍将其视为二维数组结构
您所面临的问题是我看到的是您汇总数据的方式。
举了一个小例子来说明如何总结二维数组中的列值
public class NewClass {
public static void main(String[] args){
NewClass newClass = new NewClass();
newClass.method();
}
public void method(){
int[][] array = new int[6][6];
int[] results = new int[6];
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array.length; j++) {
if(i <= array.length/2)
array[i][j] = j * i;
else
array[i][j] = j+i;
}
for(int i =0; i < results.length; i++)
{
for(int j = 0; j < array.length; j++)
{
results[i] += array[j][i];
}
}
System.out.println("Array" + Arrays.deepToString(array));
System.out.println("results: " + Arrays.toString(results));
}
}
这会产生这样的输出
数组[[0,0,0,0,0,0],[0,1,2,3,4,5],[0,2,4,6,8,10],[0, 3,6,9,12,15],[4,5,6,7,8,9],[5,6,7,8,9,10]]
结果:[9,17,25,33,41,49]
请注意,当值包含列表/数组时,TreeMap也是一组2列表,因此基于其余代码的类似aproach是可用的
答案 2 :(得分:0)
您可以iterate
通过Map
并使用Java 8&#39; stream
计算总和,例如:
Map<String, double[]> data = new TreeMap<>(); // Map of double arrays
for(Map.Entry<String, double[]> entry : data.entrySet()){
System.out.println(Arrays.stream(entry.getValue()).sum());
}
<强>更新强>
如果您希望在乘法后使用sum
列,则可以使用以下内容:
Map<String, double[]> m1 = new TreeMap<>();
Map<String, Double> m2 = new TreeMap<>();
double count[] = { 3.9, 1.2, 6.2, 1.8, 7.6, 3.8 };
double count1[] = { 1.6, 7.2, 6.2, 2.3, 1.8, 0.0 };
double count2[] = { 1.6, 5.5, 1.8, 8.8, 0.0, 0.0 };
double count3[] = { 0.0, 0.0, 0.0, 2.3, 0.0, 0.0 };
double count4[] = { 2.0, 2.2, 1.2, 3.9, 2.3, 4.4 };
double count5[] = { 3.4, 0.0, 1.9, 2.4, 0.5, 2.0 };
m1.put("apple", count);
m1.put("orange", count1);
m1.put("banana", count2);
m1.put("cherry", count3);
m1.put("lemon", count4);
m1.put("strawbarry", count5);
m2.put("apple", 2.1);
m2.put("cherry", 1.9);
m2.put("grasb", 3.0);
m2.put("strawbarry", 4.1);
for(Map.Entry<String, double[]> entry : m1.entrySet()){
if(m2.containsKey(entry.getKey())){
double multiplier = m2.get(entry.getKey());
double[] values = entry.getValue();
for(int i=0 ; i<values.length ; i++){
values[i] = values[i] * multiplier;
}
}
}
//Multiplication done, now sum the columns
double sum = 0;
for(int i = 0; i<6 ; i++){
for(Map.Entry<String, double[]> entry : m1.entrySet()){
sum += entry.getValue()[i];
}
System.out.println(sum);
sum = 0;
}