知识点:1、static void sort(byte[] a) 按照数字顺序排列指定的数组。 2、System.out.println(Arrays.toString(数组名));输出数组
题目:找出一个整数数组中子数组之和的最大值,例如:数组[1, -2, 3, 5, -1],返回8(因为符合要求的子数组是 [3, 5]);数组[1, -2, 3, -8, 5, 1],返回6(因为符合要求的子数组是 [5, 1]); 数组[1, -2, 3,-2, 5, 1],返回7(因为符合要求的子数组是 [3, -2, 5, 1])。
代码:
1 2public class TestSort { 3 public static void main(String[] args) { 4 test(); 5 6 } 7 //测试 sort()方法 8 public static void test(){ 9 int[] a = {1,-2,3,5,-1,7}; 10 int result=0; 11System.out.println("排序前的数组:"+Arrays.toString(a)); 12 Arrays.sort(a);//先排序,按升序排列后。 13 result=a[5]+a[4];//最后两个数最大,相加得整数数组中子数组之和的最大值 14 System.out.println("排序后的数组:"+Arrays.toString(a)); 15 System.out.println("整数数组中子数组之和的最大值:"+a[5]+"+"+a[4]+"="+result); 16 } 17}
结果:

