码迷,mamicode.com
首页 > 其他好文 > 详细

数组中的跳跃问题

时间:2014-10-02 13:26:42      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   strong   

本文翻译自stackoverflow,by:王奎

问题:我有一个数组:

[1,2,3,6,7, 8, 9,20, 22]

我想让它对用户下面形式显示

Numbers 1 through 3, 6 through 9, 20, 22
以下是一个完整是程序:

/**
	blog:http://www.marksaas.com
	author :marksaas
	
*/
public class ArrayTest{
public static void main(String[] args){
Integer[] A = {1, 2, 3, 6, 7, 8, 9, 20, 22};
System.out.print("Numbers ");
int start = 0, end;
while(start < A.length){
    System.out.print(A[start]);
    end = start;
    while(end + 1 < A.length && A[end + 1] == A[end] + 1)
        end++;
    System.out.print((end == start ? "":(" through " + A[end])) + ", ");
    start = end + 1;
}
	}
}
欢迎关注我的微博  ,我的微博会实时更新文章。  交流群: 

199326422



数组中的跳跃问题

标签:style   blog   http   color   io   os   ar   java   strong   

原文地址:http://www.cnblogs.com/hrhguanli/p/4003885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!