1 public class Test1 {
publicstatic void main(String[] args) {
Stringstring = "hello";
Stringstring2 = "hello";
charc[] = {'h','e','l','l','o'};
System.out.println(string==string2); //地址相同,指向堆中的同一块内存,true
//System.out.println(string.equals(c);//首先类型不同,直接false
System.out.println(string.equals(string2));//内容相同 true
System.out.println(string2.equals(new String("hello")));//内容相同 true
}
}
* 注解: == 是比较两个内存地址是否相同,相同为true ,不同为false 在字符串“常量池”中,若已经有字符串,则,直接赋值,不再新创建一个字符串,如果是new 关键字,则新创建一个不同的字符串;String类的equals被被重新复写。只要字符串相等,则返回true.
2 快排序:大到小
public class Test2 {
public static void main(String[] args) {
inta[] = {1,2,3,4,5,6,7,8};
doSomething(a,0,a.length-1);
for(int i = 0; i < a.length; i++) {
System.out.println(a[i]+ " " );
}
}
private static void doSomething(int a[],int start, int end)
{
if(start < end)
{
int position = Core(a,start, end);
doSomething(a, start, position-1);
doSomething(a, position+1, end);
}
}
private static int Core(int a[], int start, int end)
{
int x = a[end];
int i = start;
for(int j = start;j<=end-1;j++)
{
if(a[j] >= x)
{
swap(a,i,j);
i++;
}
}
swap(a, i, end);
returni;
}
privatestatic void swap(int[] a, int i, int j) {
inttemp = a[i];
a[i]= a[j];
a[j]= temp;
}
}
3 快排:小到大
public class Test3 {
publicstatic void main(String[] args) {
inta[] = {12,3,21,2,344,24,5,2};
qS(a,0, a.length-1);
for(int i = 0; i < a.length; i++) {
System.out.println(a[i]+ " ");
}
}
publicstatic void qS(int a[] , int start, int end)
{
if(start<= end)
{
intposition = Ind(a, start, end);
qS(a,start, position-1);
qS(a,position+1, end);
}
}
publicstatic int Ind(int a[], int start, int end)
{
intm_index = a[start];
while(start< end)
{
while(start< end && a[end] >= m_index)
end--;
a[start]= a[end];
while(start< end && a[start] <= m_index)
start++;
a[end]= a[start];
}
a[start]= m_index;
returnstart;
}
}
4 public class Test4 {
String string= new String("good");
char ch[] = {'a','b','c'};
public static void main(String[] args) {
Test4 test4 = new Test4();
test4.change(test4.string, test4.ch);
System.out.print(test4.string + "");
System.out.println(test4.ch);
}
public void change(String string, char ch[])
{
string = "test ok";
ch[0] = 'g';
}
}
/**
* 注解:考察的是值传递和引用传递。对于值传递,拷贝的值用完后会被释放,对原值没有任何影响:
* 对于引用传递,拷贝的是对象的引用,和原值指向的是同一个地址,即操作的是同一个对象,所以操作之间会相互影响,(类,接口,数组,枚举)引用数据类型.
* 对于String string 是值传递******,传递的是一个副本,对于ch数组,是引用传递.
* String类是一个final类,不能被继承,以及 String底层的字符数组被声明为private final char []value;所以其值不可以被修改,至于change 方法中的string = “Test ok”直接赋值方式创建,JAVA 是通过String Pool 来进行管理,即常量池。
*/
5 1 有关hashMap 和 hashTable 的区别? (ABCD)
* AhashMap 和 hashTable 都实现了Map的接口
* BhashMap 是非synchronized, 而hashTable 是synchronized
* ChashTable 使用 Enumeration ,而HashMap 使用的是Iterator
* ChashTable 直接使用对象的hashCode,hashMap重新计算hash值,而且用与代码求模
*
* 注解:
* hashMap 和 hashTable 的区别:
* 1 继承不同:
* public class hashTable extends Dictionary implements Map
* public class hashMap extends AbstractMap implement Map
* 2hashTable 中的方法是同步的,而hashMap 中的方法在缺省情况下是非同步的,在多线程并发的情况下,可以直接使用hashTable ,但要使用hashMap 的话就要自己增加同步处理,
* 3hashTable 中,key 和value都不允许出现null值;在HashMap 中,null可以作为键值,这样的键值只能有一个,可以有一个或者多个键值对应的实值为null
* 4 两个遍历的内部实现不同:hashTabe hashMap 使用 Iterator,而由于历史原因,hashTable还使用Enumeration;
* 5 哈希值的使用不同,HashTable直接使用对象的hashCode,而 hashMap重新计算hash值;
* 6hashMap 和hashTable 内部实现方式的数组的初始化大小和扩容的方式不同:
* HashMap 数组的默认大小是16 而且一定是2的指数
* HashTable 默认的大小是11,增加的方式是old*2+1;