为了账号安全,请及时绑定邮箱和手机立即绑定

6.5的课后练习程序

求6.5最后的练习小程序??

正在回答

2 回答

public class CollectiongTest {
	
	
	/**
	 * Integer排序
	 */
	public void testSort1(){
		List<Integer> integerList = new ArrayList<Integer>();
		Random random = new Random();
		Integer k;
		for(int i=0;i<10;i++){
			do{
				k = random.nextInt(100);
			}while(integerList.contains(k));
			integerList.add(k);
		}
		System.out.println("------------排序前-----------");
		for(Integer integer:integerList){
			System.out.println("包含数字:"+integer);
		}
		Collections.sort(integerList);
		System.out.println("------------排序后-----------");
		for(Integer integer:integerList){
			System.out.println("包含数字:"+integer);
		}
		
	}
	/**
	 * 字符串排序
	 */
	public void testSort2(){
		List<String> stringList = new ArrayList<String>();
		stringList.add("microsoft");
		stringList.add("apple");
		stringList.add("google");
		stringList.add("huawei");
		System.out.println("------------排序前-----------");
		for(String string:stringList)
			System.out.println("包含字符串:"+string);
		Collections.sort(stringList);
		System.out.println("------------排序后-----------");
		for(String string:stringList)
			System.out.println("包含字符串:"+string);
	}
	/**
	 * 字符串排序
	 */
	// 随机生成长度不等的字符串
	public void testSort3(){
		List<String> stringList = new ArrayList<String>();
		Random random = new Random();
		Integer k;
		for(int i=0;i<10;i++){
			do{
				k = random.nextInt(10);
			}while(k == 0);
			String temp = testString(k);
			stringList.add(temp);
		}
		System.out.println("------------排序前-----------");
		for(String string:stringList)
			System.out.println("包含字符串:"+string);
		Collections.sort(stringList);
		System.out.println("------------排序后-----------");
		for(String string:stringList)
			System.out.println("包含字符串:"+string);
		
	}
	// 方法返回长度不等的字符串
	public String testString(Integer count){
		String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		StringBuffer sb = new StringBuffer();
		int length = str.length();
		Random random = new Random();
		for(int i=0;i<count;i++){
			int num = random.nextInt(length);
			sb.append(str.charAt(num));
		}
		String temp = sb.toString();
		return temp;
	}

	public static void main(String[] args) {
		CollectiongTest ct = new CollectiongTest();
//		ct.testSort1();
//		ct.testSort2();
		ct.testSort3();

	}

}


1 回复 有任何疑惑可以回复我~
#1

慕仔7135344 提问者

非常感谢!
2016-11-11 回复 有任何疑惑可以回复我~
#2

慕粉4022211 回复 慕仔7135344 提问者

上面的代码有错的,漏了一个do...while循环防止字符串出现重复,你把字符串长度设为1就会发现出现重复字符串了
2016-11-17 回复 有任何疑惑可以回复我~

你的代码有问题啊     执行不出来

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409776    人
  • 解答问题       4339    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

6.5的课后练习程序

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信