您的位置:首页 > 职场人生

JAVA最新面试题分享

2018-03-06 17:16 531 查看
用程序给出随便大小的10 个数,序号为1-10,按从小到大顺序输出,并输出相应的序号。【基础】
答:代码如下:
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class RandomSort {
public static void printRandomBySort() {
Random random = new Random(); // 创建随机数生成器
List list = new ArrayList();
// 生成10个随机数,并放在集合list 中
for (int i = 0; i < 10; i++) {
list.add(random.nextInt(1000));
}
Collections.sort(list); // 对集合中的元素进行排序
Iterator it = list.iterator();
int count = 0;
while (it.hasNext()) { // 顺序输出排序后集合中的元素
System.out.println(++count + ": " +it.next());
}
}
public static void main(String[] args) {
printRandomBySort();
}
}
80、用JAVA 实现一种排序,JAVA 类实现序列化的方法? 在COLLECTION 框架中,实现比较要实现什么样的接口?【基础】
答:用插入法进行排序代码如下:
package test;
import java.util.*;
class InsertSort {
ArrayList al;
public InsertSort(int num,int mod) {
al = new ArrayList(num);
Random rand = new Random();
System.out.println("The ArrayList SortBefore:");
for (int i=0;i<num ;i++ ){
al.add(new Integer(Math.abs(rand.nextInt()) % mod +
1));
System.out.println("al["+i+"]="+al.get(i));
}
}
public void SortIt(){
tempInt;
int MaxSize=1;
for(int i=1;i<al.size();i++){
tempInt = (Integer)al.remove(i);
if(tempInt.intValue() >=
((Integer)al.get(MaxSize-1)).intValue()){
al.add(MaxSize,tempInt);
MaxSize++;
System.out.println(al.toString());
}else{
for (int j=0;j<MaxSize ;j++ ){
if (((Integer)al.get(j)).intValue()
>=tempInt.intValue()){
al.add(j,tempInt);
MaxSize++;
System.out.println(al.toString());
break;
}
}
}
}
System.out.println("The ArrayList SortAfter:");
for(int i=0;i<al.size();i++){
System.out.println("al["+i+"]="+al.get(i));
}
}
public static void main(String[] args){
InsertSort is = new InsertSort(10,100);
is.SortIt();
}
}
JAVA 类实现序例化的方法是实现java.io.Serializable接口;Collection 框架中实现比较要实现Comparable 接口和Comparator 接口。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA面试题