我有一个二维的字符串数组,我想找出每13个元素的最大值。
数组为arrayString dateString price2。我想要price1 0-12的最大值,然后是price1 1-13,然后是price1 2-14,等等。
{1,2,3,4,5,6,7,8,9,9,8,11,6,5,4,3,2,1}
13 {1,2,3,4,5,6,7,8,9,10,9,8}的第一份名单将返回10
13 {2,3,4,5,6,7,8,9,10,9,8,7, 11 }的第二份名单将返回11
第三份名单列出13名{3,4,5,6,7,8,9,10,9,8,7, 11 ,6}将返回11人等等。
编辑对不起,这有点混乱,数组是一个二维字符串数组,第一列是日期,第二和第三列是双倍。我想找第二列的最大值和第三列的最小值。
我得到的是:
String str = "";
for(int ii = 1 ; ii < array.length ; ii++){
str = str+array[ii][2]+",";
if(ii==13){
str = Math.max(str.substring(0, str.lastIndexOf(',', str.lastIndexOf(',') - 1)));
System.out.println(str);
}
}发布于 2011-12-15 03:22:39
这是我的解决方案(如果您正在处理整数数组)。
但是--你刚开始说的是String dateString price2和
后来你举了一个例子:第一个13 {1,2,3,4,5,6,7,8,9,9,8,7}将返回10,第二个列表13 {2,3,4,5,6,8,9,9,8,7,11 }将返回11,第三列13 {3,4,5,6,8,9,10,9,8,11}将返回11等--请清楚地说明这一点。
代码:
public class SO_MinMaxArrayGroups {
public static void main(String[] args) {
int arr[] = new int[] { 1, 2, -3, 4, 5, 6, 17, 13, 14, 14, 14, 12, 1, 2, 2, 4, 3, 4,1, 2, 3, 4, 5, 6, 12, 13, -14, 14, 14, 12, 1, 2, 2, 4, 3, 4,1,4,7 };
for (int i = 0; i < 3; i++) {
int max = arr[13*i];
int min = arr[13*i];
for (int j = i * 13; j < (i + 1) * 13; j++) {
//System.out.println("Checking :" + arr[j]);
if (arr[j] > max) {
max = arr[j];
}
if (arr[j] < min) {
min = arr[j];
}
}
System.out.println("Secion :"+(i+1)+" Max : " + max + " Min : " + min);
}
}
}结果:
Secion :1 Max : 17 Min : -3
Secion :2 Max : 13 Min : 1
Secion :3 Max : 14 Min : -14发布于 2011-12-15 03:14:44
如果您处理的是数字,数组不应该存储字符串( 1,11和2( '2‘> '11’) :-)之间比较比较困难
您可以将复制范围放入另一个数组,并在范围之外创建一个树集,该树集可用于提取最小和最大数字。
哈哈!
https://stackoverflow.com/questions/8514636
复制相似问题