class Test
{
public static void main(String[] args)
{
//定义一个装整型数组的ArrayList集合,放入数组的所有子集
// ArrayList<int[]> al = new ArrayList<int[]>();
int[] arr = {1,-2,3,10,-4,7,2,-5};
//al = getSubArrays(arr);
int maxSum = getMaxSumArray(arr);
System.out.println("maxSum = "+maxSum);
}
/*
定义一个功能,找出子数组和的最大值
*/
public static int getMaxSumArray(int[] arr)
{
//定义一个装整型数组的ArrayList集合,放入数组的所有子集
ArrayList<int[]> al = new ArrayList<int[]>();
al = getSubArrays(arr);
int maxSum = arraySum(al.get(0));
for (Iterator<int[]> it = al.iterator();it.hasNext() ; )
{
int[] temp = it.next();
if (arraySum(temp)>maxSum)
{
maxSum = arraySum(temp);
}
}return maxSum;
}
/*
定义一个给数组求和的功能
*/
public static int arraySum(int[] arr)
{
int sum = 0;
for (int x=0;x<arr.length ;x++ )
{
sum = sum+arr[x];
}return sum;
}
/*
定义一个获取数组所有子集的功能
*/
public static ArrayList<int[]> getSubArrays(int[] arr)
{
ArrayList<int[]> al = new ArrayList<int[]>();
for (int x=0;x<arr.length ;x++ )
{
for (int y=0,z=arr.length-x;z!=arr.length+1 ;y++,z++ )
{
int[] temp = Arrays.copyOfRange(arr,y,z);
al.add(temp);