"Java笔试算法题及答案.doc:实现数字输入后的升降序排序"

版权申诉
0 下载量 12 浏览量 更新于2024-03-03 收藏 150KB DOC 举报
。。" 答案: ``` import java.util.*; public class Bycomma { public void bycomma(String sysin){ String[] split = sysin.split(","); int[] result = new int[split.length]; for(int i = 0; i<result.length; i++){ result[i] = Integer.parseInt(split[i]); } Arrays.sort(result); //asc 升序输出 for(int i=0; i<result.length; i++){ System.out.print(result[i]); if(i != result.length-1){ System.out.print(","); } } } } ``` 题目2. 使用java实现单例模式,要求线程安全 ``` public class Singleton { private volatile static Singleton uniqueInstance; private Singleton() {} public static Singleton getUniqueInstance() { if (uniqueInstance == null) { synchronized (Singleton.class) { if (uniqueInstance == null) { uniqueInstance = new Singleton(); } } } return uniqueInstance; } } ``` 题目3. 编写一个程序按顺序类1-2-3-4生成输出1 2 4 3 6 8 5 10...每两位数之间的间隔是前面数的倍数 ``` public class NumberSequence { public void generateSequence() { int current = 1; for (int i = 1; i < 100; i++) { System.out.print(current + " "); if (i % 2 == 0) { current *= 2; } else { current *= 3; } } } } ``` 以上就是Java笔试算法题及答案。