本文翻译自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; } } }欢迎关注我的微博 ,我的微博会实时更新文章。 交流群: