Java常见面试题(一)

1 实现一个 字符串的 reverse函数和 replace函数.

reverse:

        public static void stringReverser() {
		String strOld = "aaabbb";
		String strNew = new StringBuffer(a).reverse().toString();
		System.out.println(strNew);
	}

replace:
	public static String replace(String text, String repl, String with) {
		if ((text == null) || (repl == null) || (with == null)
				|| (repl.length() == 0) || (max == 0)) {
			return text;
		}

		StringBuffer buf = new StringBuffer(text.length());
		int start = 0;
		int end = 0;

		while ((end = text.indexOf(repl, start)) != -1) {
			buf.append(text.substring(start, end)).append(with);
			start = end + repl.length();
		}

		buf.append(text.substring(start));
		return buf.toString();
	}

2 ArrayList 和 Vector的区别,HashMap和HashTable的区别.
就 ArrayList 和 Vector从两方面说:
一: ArrayList不是线程安全的,Vector是线程安全的,也就是说 Vector是线程同步的.具体的可以参见Vector的实现代码:

二: size增长.当需要增长时,Vectory为原来默认的一倍

 private void ensureCapacityHelper(int minCapacity) {
	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    Object[] oldData = elementData;
	    int newCapacity = (capacityIncrement > 0) ?
		(oldCapacity + capacityIncrement) : (oldCapacity * 2);
    	    if (newCapacity < minCapacity) {
		newCapacity = minCapacity;
	    }
            elementData = Arrays.copyOf(elementData, newCapacity);
	}
    }

ArrayList是原来的一半.
   public void ensureCapacity(int minCapacity) {
	modCount++;
	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    Object oldData[] = elementData;
	    int newCapacity = (oldCapacity * 3)/2 + 1;
    	    if (newCapacity < minCapacity)
		newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
	}
    }

就 HashMap 和 HashTable 而言.
一: HashMap是线程不安全的,HashTable是线程安全的

二: HashMao 是可以用 null作为key和value的

3 char型能不能储存一个中文汉子?为什么?
答: 是能够储存一个中文汉字的,java使用的是unicode编码.一个char占16个字节,所以存储一个中文没问题哦.

4 多线程有几种实现方案?同步呢?
答: 有两种实现方案:
分别是继承 Runnable接口和Thread类.
同步也有两种实现方案 分别是 synchronized和notify

Posted in java学习 | Tags:

1 Comments.

  1. timberland uk 说:

    都快忘记的差不多了!

Leave a Reply

[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Get Adobe Flash playerPlugin by wpburn.com wordpress themes