24、合并兩個無序數組,並把合并後的升序數組寫入到a.txt中,降序數組寫入到b.txt中。要求去掉重複元素

來源:互聯網
上載者:User

寫這個程式之前,先總結一下最近遇到的筆試題的小問題:

 

1、Math.round()這個函數,見過好多次,可是筆試的時候,還是拿不準答案。。下面貼一下自己的實驗~

math.round(-10.6)=-11。 但是math.round(-10.5)傳回值為什麼是-10 而不是-11 不應該四舍五嗎??

 

public class TestRound {public static void main(String args[]){System.out.println(Math.round(11.4));//11System.out.println(Math.round(-11.4));//-11System.out.println(Math.round(11.5));//12System.out.println(Math.round(-11.5));//-11}}

我們看看原始碼:

看看原始碼public static long round(double a) {return (long)floor(a + 0.5d);    }

可以看到這個round()方法實現的是:就是括弧內的數+0.5之後,向下取值(取第一個比它小的整數,負數時候要特別小心),比如:round(3.4)就是3.4+0.5=3.9,向下取值是3,所以round(3.4)=3; 那麼round(-10.5)就是-10.5+0.5=-10,向下取值就是-10,所以round(-10.5)=-10

 

 

2、關於equals()也是看過好多和==的比較,但是還是有未注意到的地方

比如:

 

3、abstract方法有{}嗎?

沒有,抽象方法的實現:

abstract void f();

 

一個抽象類別中可以沒有抽象方法;

也可以有一個或多個抽象方法;

一個類A繼承一個抽象類別,必須實現全部抽象方法,否則A 依然必須是abstact的

 

 

4、介面的修飾符?以下哪些不可以使用??

public  protected private static final

介面本身只能使用public 修飾

介面中的欄位和方法,預設的:

    欄位預設不寫,被解釋為:public static final

    方法預設不寫,被解釋為:public abstract

 

5、JAVA的變數命名規則?

java變數名,最多包含:字母、數字,底線,貨幣符號。並且,數字不能開頭

java中的變數名主要遵循五個規則:
  
  1、只能以字母、“_”或“$”符號作為變數名開頭。
  
  2、變數中可包含數字,但不能以數字開頭
  
  3、除了“_”和“$”符號以外,變數中不能包含其他特殊字元。
  
  4、不能用class、int、String、public等java關鍵字做為變數名。
  
  5、在java中變數名嚴格區分大小寫,例如:name和Name就是兩個不同的變數。

6、int a[] = new int(25);

求a[24]?

應該是0

 

7、構造方法,不能被繼承,所以不能被重寫。但是能被重載

 

8、合并兩個無序數組

方法1、使用System.arraycopy()複製到一個新的數組中。這種方法不能去掉重複元素,並且,合并的數組無序。

import java.util.Arrays;public class ArrayCopy {public static void main(String args[]){int a[]={9,5,17,3};int b[] = {8,17,4,6};copyTest(a,b);}public static void copyTest(int a[],int b[]){int temp[] = new int[a.length+b.length];System.arraycopy(a, 0, temp, 0, a.length);System.arraycopy(b, 0, temp, a.length, b.length);System.out.println(Arrays.toString(temp));//結果為:[9, 5, 17, 3, 8, 17, 4, 6]}}

 

 

改進,方法2、使之去掉重複元素

import java.util.Arrays;import java.util.HashSet;import java.util.Iterator;public class ArrayCopy {public static void main(String args[]){int a[]={9,5,17,3};int b[] = {8,17,4,6};copyTest(a,b);}public static void copyTest(int a[],int b[]){//int temp[] = new int[a.length+b.length];HashSet<Integer> hs = new HashSet();//使用set可以自動去掉重複的元素//int i;//int j;int len1 = a.length;int len2 = b.length;for(int i=0, j=0;i<len1&&j<len2;i++,j++){//使用for迴圈進行控制    hs.add(a[i]);//添加元素    hs.add(b[j]);//添加元素}//使用集合進行輸出Iterator<Integer> it = hs.iterator();while(it.hasNext()){System.out.println(it.next() + " ");//結果為:17 3 4 5 6 8 9}}

方法3、使合并後的數組有序

import java.util.Arrays;public class ArrayCopy {public static void main(String args[]){int a[]={9,5,17,3};int b[] = {8,17,4,6};copyTest(a,b);}public static void copyTest(int a[],int b[]){int temp[] = new int[a.length+b.length];System.arraycopy(a, 0, temp, 0, a.length);System.arraycopy(b, 0, temp, a.length, b.length);System.out.println(Arrays.toString(temp));//結果為:[9, 5, 17, 3, 8, 17, 4, 6]Arrays.sort(temp);System.out.println(Arrays.toString(temp));//結果為:[3, 4, 5, 6, 8, 9, 17, 17]}}

方法4、合并後的數組後,使用快排

import java.util.Arrays;public class ArrayCopy {public static void main(String args[]){int a[]={9,5,17,3};int b[] = {8,17,4,6};int c[] = copyTest(a,b);quickSort(c,0,c.length-1);System.out.println(Arrays.toString(c));}public static int[] copyTest(int a[],int b[]){int temp[] = new int[a.length+b.length];System.arraycopy(a, 0, temp, 0, a.length);System.arraycopy(b, 0, temp, a.length, b.length);System.out.println(Arrays.toString(temp));//結果為:[9, 5, 17, 3, 8, 17, 4, 6]return temp;}public static void writeToFile(){}//快排/*public static void quickSort(int data[],int low,int high){int i=low,j=high;int key = data[low];while(low < high){while((low < high) && (data[high]>=key))//不要忘了等於high--;        data[low] = data[high];    while((low <high) && (data[low]<=key))   low++;        data[high] = data[low];}data[low] = key;    if(j != high)quickSort(data,high+1,j);    if(i != low)quickSort(data,i,low-1);}*/private static int partition(int data[],int low,int high){int key = data[low];while(low<high){while(low<high && data[high]>=key)high--;data[low] = data[high];while(low<high && data[low]<=key)low++;data[high] = data[low];}data[low] = key;return low;}private static void quickSort(int data[],int low,int high){int q;if(low<high){ q = partition(data,low,high);     quickSort(data,q+1,high);     quickSort(data,low,q-1);}}}

 備忘:

給定含有1001個元素的數組,其中存放了1-1000之內的整數,只有一個整數是重複的,請找出這個數

分析

求出整個數組的和,再減去1-1000的和

代碼

找出出現奇數次的元素

給定一個含有n個元素的整型數組a,其中只有一個元素出現奇數次,找出這個元素。

分析

因為對於任意一個數k,有k ^ k = 0,k ^ 0 = k,所以將a中所有元素進行異或,那麼個數為偶數的元素異或後都變成了0,只留下了個數為奇數的那個元素。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.