简体   繁体   English

Java中的bin 2D数组

[英]bin 2D arrays in java

I have an interesting problem. 我有一个有趣的问题。 I have an 2D array of say n elements. 我有说n个元素的2D数组。 i want to create a function that "bins" the array elements according to a number. 我想创建一个函数,根据一个数字“绑定”数组元素。 Given 给定

String [][] theArray = 
            {
             {"word1", "3.5"},
             {"word2", "2.4"},
             {"word3", "1.2"},
             {"word4", "0.5"},
             {"word5", "0.2"}
            };

If the "binning" number were 1 then theArray would be the same. 如果“ binning”数为1,则theArray将相同。 If the "binning" number were 2 then theArray would become 如果“ binning”数为2,则theArray将变为

newArray ={{"word1 word2", "5.9"},{"word3 word4", "1.7"},{"word5", "0.2"}}

Notice that the first element of each subarray is the concatenation of the first elements of the original array. 请注意,每个子数组的第一个元素是原始数组的第一个元素的串联。 The second element of each subarray is the addition of the second elements of the original array. 每个子数组的第二个元素是原始数组的第二个元素的加法。

Also, if the mod of theArray.length/"binning" number is greater than 0 the number of elements of the new array should be (theArray.length/"binning" number)+1. 另外,如果theArray.length /“ binning”的mod大于0,则新数组的元素数应为(theArray.length /“ binning”的数字)+1。 The last element should be the binning of the remaining elements. 最后一个元素应该是其余元素的合并。

I tried to do something like this 我试图做这样的事情

public String [][] binArray(String [][]theArray, int theBinningNumber)
{
 //here i would do some nested loops, but to be honest with you guys, all my trials were
 //far from succesful
}

Thanks so much for your help 非常感谢你的帮助

Please have a look at this sample, 请看一下这个样本,

public static void main(String []args)
   {
   String [][] theArray = 
            {
             {"word1", "3.5"},
             {"word2", "2.4"},
             {"word3", "1.2"},
             {"word4", "0.5"},
             {"word5", "0.2"}
            };

    String newArray[][]=binArray(theArray,2);
    for(String []ar : newArray)
     {
       System.out.println(ar[0] + " " + ar[1]);
     }
   }

   public static String [][]binArray(String [][]theArray,int theBinningNumber)
   {
    //Determine the size (length) of new array
    int newSize=theArray.length/theBinningNumber;
    if(theArray.length % theBinningNumber !=0)
       {
       newSize++;
       }

     //Define new array 
     String [][]newArray=new String[newSize][];
     int theNewIndex=0;

     for(int index=0;index<theArray.length;index+=theBinningNumber)
      {
          String []ar=new String[] {"",""};  
          double value=0;
          for(int binIndex=index;
                   binIndex<(index+theBinningNumber) 
                            && binIndex<theArray.length;
                                            binIndex++)
           {
               value = value + Double.parseDouble(theArray[binIndex][1]);
               ar[0]=ar[0] + " " + theArray[binIndex][0];
               ar[1]=String.valueOf(value);
            }
          newArray[theNewIndex++]=ar;
       }
      return newArray;        
   }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM