Java的集合笔试题

1、下列哪个集合操作符返回两个查询所选择的所有的行。(选择 1 项) B
A Union
B Union all
C Union only
D connect by
Union 集合操作符返回两个查询所选择的去除重复行。
2在 Oracle 中,当需要使用显式游标更新或删除游标中的行时,UPDATE 或 DELETE 语句必须使用()子句。 解答:C
A.WHERE CURRENT OF
B.WHERE CURSOR OF
C.FOR UPDATE
D.FOR CURSOR OF
为了对正在处理(查询)的行不被另外的用户改动,ORACLE 提供一个 FOR UPDATE 子句来对所选择的行进行锁住。该需求迫使 ORACLE 锁定游标结果集合的行,可以防止其他事务处理更新或删除相同的行,直到您的事务处理提交或回退为止。如果使用 FOR UPDATE 声明游标,则可在 DELETE 和 UPDATE 语句中使用 WHERE CURRENT OF cursor_name子句,修改或删除游标结果集合当前行对应的数据库表中的数据行。
3、JDK1.5 中支持的 for 循环的语法
解答:for(type element : array) {
System.out.println(element)
}
type 集合(不仅仅指 Collection,也包含普通的数组)中元素的数据类型
element 遍历到的元素
array 集合对象本身(当然不只是 Collection)
4、1.编程实现:二分搜索算法

public class SearchTest {
被搜索数据的大小 
private static final int size = 5000000;
public static void main(String[] args) {
long[] data = new long[size];
// 添加测试数据
for (int k = 0; k < data.length; k++) {
data[k] = k;
}
// 要查找的数据
long target = 4970002;
binaryFindTest(data, target);
}
* 二分搜索算法实现
*
* @param data
* 数据集合
* @param target
* 搜索的数据
* @return 返回找到的数据的位置,返回-1 表示没有找到。
*/
public static int binaryFind(long[] data, long target) {
int start = 0;
int end = data.length - 1;
 while (start <= end) {
int middleIndex = (start + end) / 2;
if (target == data[middleIndex]) {
return middleIndex;
}
if (target >= data[middleIndex]) {
start = middleIndex + 1;
} else {
end = middleIndex - 1;
}
}
return -1;
}
/**
* 二分搜索测试
*
* @param data
* 数据集合
* @param target
* 搜索的数据
*/
public static void binaryFindTest(long[] data, long target) {
long start = System.nanoTime();
int result = binaryFind(data, target);
long end = System.nanoTime();
System.out.println("binary search position:" + result);
System.out.println("binary search time:" + (end - start));
}
}

2.编程实现:线程 A 向队列 Q 中不停写入数据,线程 B 从队列 Q 中不停读取数据(只要 Q 中有数据)。
解答:
接口中有两个一个是向队列中写 push 方法 一个是从队列中读。

public interface StackInterface
{
public void push(int n);
public int[] pop();
}
上边接口的实现类。
public class SafeStack implements StackInterface {
private int top = 0;
private int[] values = new int[10];
private boolean dataAvailable = false;
public void push(int n) {
synchronized (this) {
while (dataAvailable) // 1
{
try {
wait();
} catch (InterruptedException e) {
// 忽略 //2
}
}
values[top] = n;
System.out.println("压入数字" + n + "步骤 1 完成");
top++;
dataAvailable = true;
notifyAll();
System.out.println("压入数字完成");
}
}
 public int[] pop() {
synchronized (this) {
while (!dataAvailable) // 3
{
try {
wait();
} catch (InterruptedException e) {
// 忽略 //4
}
}
System.out.print("弹出");
top--;
int[] test = { values[top], top };
dataAvailable = false;
// 唤醒正在等待压入数据的线程
notifyAll();
return test;

5、.链表具有的特点是:(选择 3 项)
A、不必事先估计存储空间
B、可随机访问任一元素
C、插入删除不需要移动元素
D、所需空间与线性表长度成正比
解答:ACD
6、用程序给出随便大小的 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();
}
}

7、用 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<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 Sort After:");
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 接口。
8、TreeSet 里面放对象,如果同时放入了父类和子类的实
例对象,那比较时使用的是父类的 compareTo 方法,还是使
用的子类的 compareTo 方法,还是抛异常!
(应该是没有针对问题的确切的答案,当前的 add 方法放入的是哪个对象,就调用哪个对象 的 compareTo 方法,至于这个 compareTo 方法怎么做,就看当前这个对象的类中是如何编写 这个方法的) 实验代码:

public class Parent implements Comparable { 
 private int age = 0;  
public Parent(int age){  
 this.age = age;  }  
public int compareTo(Object o) {  
System.out.println("method of parent");  
 Parent o1 = (Parent)o; 
  return age>o1.age?1:age<o1.age?-1:0;  } 
} 
public class Child extends Parent { 
 
 public Child(){   
super(3);  } 
 public int compareTo(Object o) { 
 System.out.println("method of child"); // 
 Child o1 = (Child)o;    return 1; 
 } } 
public class TreeSetTest { 
 
public static void main(String[] args) {  
 	TreeSet set = new TreeSet();  
set.add(new Parent(3));  
  	set.add(new Child());   
set.add(new Parent(4));  
System.out.println(set.size());  } 
} 

9、ArrayList 如何实现插入的数据按自定义的方式有序存放
编程思路是:实现一个类对 ArrayList 进行包装,当程序试图向 ArrayList 中放入数据时, 程序将先检查该元素与 ArrayList 集合中其他元素的大小,然后将该元素插入到指定位置。

class MyBean implements Comparable{  
public int compareTo(Object obj){  
 if(! obj instanceof MyBean)   
 		throw new ClassCastException()。   
MyBean other = (MyBean) obj;  
 return age > other.age?1:age== other.age?0:-1;  
} 
} class MyTreeSet { 
 private ArrayList  datas = new ArrayList();   
 public void add(Object obj){ 
  for(int i=0;i<datas.size();i++){    
if(obj.compareTo(datas.get(i) != 1){    
 datas.add(i,obj); 
  			 }   
}  
} 
发布了33 篇原创文章 · 获赞 50 · 访问量 2675

猜你喜欢

转载自blog.csdn.net/qq_43107323/article/details/103692859
今日推荐