在Android studio中,测试输出数组中最大子数组的和

本文介绍了一个Java类Array,其中包含一个名为maxArr的方法,用于计算输入数组的最大子序列和。通过测试类ArrayTest验证了maxArr方法的正确性,确保其能准确找出数组中具有最大和的连续子序列。
 1  1 package com.example.tony.testforgit;
 2  2 
 3  3 /**
 4  4  * Created by Tony on 2017/3/17.
 5  5  */
 6  6 public class Array {
 7  7     public  int maxArr(int[] arr){
 8  8         int newSum = arr[0];
 9  9         int maxSum = arr[0];
10 10         for(int i=1;i<arr.length;i++){
11 11             newSum += arr[i];
12 12             if(newSum<arr[i]){
13 13                 newSum = arr[i];
14 14             }
15 15             if(maxSum<newSum){
16 16                 maxSum = newSum;
17 17             }
18 18         }
19 19         return maxSum;
20 20     }
21 21 }

生成测试类,编辑测试用例

 1 package com.example.tony.testforgit;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 
 6 import static org.junit.Assert.*;
 7 
 8 /**
 9  * Created by Tony on 2017/3/17.
10  */
11 public class ArrayTest {
12     private Array array;
13 
14     int arr1[] ={1,2,3,-1,2};
15     
16     @Before
17     public void setUp() throws Exception {
18        array = new Array();
19     }
20 
21     @Test
22     public void testMaxArr() throws Exception {
23         assertEquals(7d,array.maxArr(arr1),0);
24 
25 
26     }
27 }

测试结果:成功

转载于:https://www.cnblogs.com/lippman/p/6567948.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值