使用循环判断数组元素是否对称

时间:2020-10-29 09:20:36   收藏:0   阅读:23
@Test
    public void b() {

        int[] arr = new int[]{1, 2, 3, 4, 5, 4, 3, 2, 1};

        int start = 0;
        int end = arr.length - 1;
        while (start <= end) {

            if (arr[start] != arr[end]) {

                System.out.println("此数组不对称");
                System.exit(0);

            } else {
                start++;
                end--;
            }

        }

        System.out.println("此数组是对称的!!");
    }

判断字符串元素是否对称

@Test
    public void a() {

        //将字符串反转,反转之后的字符串和原本的比较
        String str = "ABCCBA";
        StringBuilder strRev = new StringBuilder(str).reverse();
        if (str.contentEquals(strRev)) {
            System.out.println("该字符串是对称的!");
        } else {

            System.out.println("该字符串不是对称的!");
        }


    }
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!