排序

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;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值