java常见面试题
java常见面试题
用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 sort before:”);
for (int i=0;i
al.add(new integer(math.abs(rand.nextint()) % mod + 1));
system.out.println(”al["+i+"]=”+al.get(i));
}
}
public void sortit()
{
integer tempint;
int maxsize=1;
for(int i=1;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
if
(((integer)al.get(j)).intvalue()>=tempint.intvalue())
{
al.add(j,tempint);
maxsize++;
system.out.println(al.tostring());
break;
}
}
}
}
system.out.println(”the arraylist sort after:”);
for(int i=0;i
system.out.println(”al["+i+"]=”+al.get(i));
}
}
public static void main(string[] args)
{
insertsort is = new insertsort(10,100);
is.sortit();
}
}