Java中數組常用方法的總結

Java

中數組常用方法的總結

 

Java

static

System.arraycopy(),

for

多,

     

System.arraycopy()

針對所有的類型做了重載,需要

5

個參數。

 

 

    

第一個參數:源數組。

 

 

    

第二個參數:偏移量

,

即從哪個位置開始複製的索引。

 

 

    

第三個參數:目標數組。

 

 

    

第四個參數:偏移量。

 

 

    

第五個參數:要從源數組中複製到目標數組元素的個數

,

一般情況下爲目標數組的長度。

 

 

例:

 

 

    

A

數組中複製元素到

B

數組?

 

 

    

public class Practice { 

 

        

public static void main(String[] args){ 

 

            

String[] A = {"H","e","l","l","o"}; 

 

            

String[] B = new String[3]; 

 

            

System.arraycopy(A, 0, B, 1, B.length - 1); 

 

          

for(int i = 0; i < B.length; i ++){ 

 

                 

System.out.print(B[i] + " "); 

 

            

 

      

 

    

 

    

運行結果爲:

null H e; 

 

二、

Array.Fill(

數組名,值

)

此方法用於填充數組。

 

 

    

例:

 

 

       

現向已知數組

A

中填充值?

 

 

       

public class Practice { 

 

             

public static void main(String[] args){ 

 

                   

String[] A = new String[5]; 

 

                   

Arrays.fill(A, "Hello"); 

 

                   

for(int i = 0; i < A.length; i ++){ 

 

                 

System.out.print(A[i] + " "); 

 

              

 

         

 

       

 

       

運行結果爲:

 

 

                  

Hello Hello Hello Hello Hello 

 

三、

Array.equals(

數組名

A

,數組名

B),

比較兩個數組

A

B

是否相等,此方法針對所有基本類型與

object

都作了

重載,例如比較兩個數是否相等用

Integer.equals()

方法

……

,通過

object.equals()

方法比較數組是否相等,是

基於內容的。

 

 

    

例:

 

 

       

三個已知數組的比較?

 

 

       

public class Practice { 

 

       

public static void main(String[] args){ 

 

                 

String[] A = {"1","2","3"}; 

 

               

String[] B = {"

","

","

"}; 

 

                 

String[] C = {"1","2","3"}; 

 

                 

System.out.println(Arrays.equals(A, B)); 

 

                 

System.out.println(Arrays.equals(A, C)); 

 

           

 

       

 

       

運行結果爲:

false 

 

                            

true 

 

四、

Array.asList()

此方法用於輸出數組中的所有數

 

    

例:

 

 

       

輸出已知數組

A

中的所有數?

 

 

       

public class Practice { 

 

               

public static void main(String[] args){ 

 

                       

String[] A = {"H","e","l","l","o"}; 

 

                     

System.out.println(Arrays.asList(A)); 

 

               

 

       

 

       

運行結果爲:

 

 

                  

[H, e, l, l, o]; 

 

五、數組中的排序

 

 

    

使用內置的排序方法,就可以對任意的基本類型數組排序,也可以對任意的對象數組進行排序,只要該對象

實現了

Comparable

接口或具有相關聯的

Comparator

接口

 

 

    

例:

 

 

      

已經數組

String[] A = {"A","B","c","D","e","f","G","H"};

現對數組

A

進行排序,要求忽略大小寫排序?

 

 

      

分析:

String

默認的排序方法,第一步是將大寫字母開頭的詞均放在小寫字母頭的詞的前面,然後才進行

排序。

 

 

            

如:

String[] A = {"A","B","c","D","e","f","G","H"}; 

 

                 

Arrays.sort(A); 

 

                   

System.out.println(Arrays.asList(A)); 

 

            

運行結果爲:

[A, B, D, G, H, c, e, f]; 

 

      

如果想忽略大小寫,可以通過自定義排序方式來實現:

 

 

            

如:

 

 

                

public class Practice1 implements Comparator{   

 

                        

public static void main(String[] args){ 

 

                               

String[] A = {"A","B","c","D","e","f","G","H"}; 

 

                               

Arrays.sort(A,new Practice1()); 

 

                               

System.out.println(Arrays.asList(A)); 

 

                        

 

                        

public int compare(Object o1, Object o2) { 

 

                               

String s1 = (String)o1; 

 

                               

String s2 = (String)o2; 

 

                               

return s1.toLowerCase().compareTo(s2.toLowerCase()); 

 

                        

 

                

 

            

運行結果爲:

[A, B, c, D, e, f, G, H]; 

 

 

comparable comparator 

網上找的資料保留下一個類實現了

Camparable

接口則表明這個類的對象之間是可以

相互比較的,這個類對象組成的集合就可以直接使用

sort

方法排序。

 Comparator

可以看成一種算法的實現,將算法和數據分離,

Comparator

也可以在下面兩種環境

下使用:

 1

、類的設計師沒有考慮到比較問題而沒有實現

Comparable

,可以通過

Comparator

來實現排序而不必改變對象本身

 2

可以使用多種排序標準,

比如升序、

降序等

   

 

六、使用

Arrays.binarySearch()

執行快速查找。

 

 

    

例:快速查找數組

A

中的元素?

 

 

        

public class Practice{   

 

                

public static void main(String[] args){ 

 

                        

String[] a = {"a","d","e","w","f"}; 

 

                      

Arrays.sort(a); 

 

                      

System.out.println(Arrays.asList(a)); 

 

                        

int index1 = Arrays.binarySearch(a, "f"); 

 

                        

System.out.println("

要查找的值存在的時候:

" + index1); 

 

                        

int index2 = Arrays.binarySearch(a, "n"); 

 

                        

index2 = -index2 - 1; 

 

                        

System.out.print("

當不存在的時候輸出該值最可能存在的位置:

" + index2); 

 

               

 

        

}   

 

    

注意:

不要對未排序的數組使用

binarySearch(); 

 

七、

Arrays.copyOf()

用法

,

這個方法是將原數組快速複製成一個新數組。如果新數組的長度小於舊數組的長度,

將截取一部分舊數組中的元素,複製給新數組,

 

 

    

反之,如果大於舊數組的長度,則將以

0

null

,或

false

給予補充。

 

 

    

例:快速複製數組?

 

 

     

public class Practice{   

 

       

public static void main(String[] args){ 

 

                 

String[] a = {"a","d","e","w","f"}; 

 

                 

String[] b = new String[4]; 

 

                 

String[] c = new String[5]; 

 

                 

String[] d = new String[6]; 

 

                 

b = Arrays.copyOf(a, b.length); 

 

                 

c = Arrays.copyOf(a, c.length); 

 

                 

d = Arrays.copyOf(a, d.length); 

 

             

System.out.println("b

數組的元素:

" + Arrays.asList(b)); 

 

                 

System.out.println("c

數組的元素:

" + Arrays.asList(c)); 

 

             

System.out.println("d

數組的元素:

" + Arrays.asList(d)); 

 

      

 

     

 

     

運行結果爲:

 

 

                

b

數組的元素:

[a, d, e, w] 

 

               

c

數組的元素:

[a, d, e, w, f] 

 

                

d

數組的元素:

[a, d, e, w, f, null] 

 

八、

Arrays.copyOfRange()

用法,這個方法與前面介紹的

Arrays.copyOf()

的用法相似,下面來通過具體實例講

解。

 

 

    

例:如何將原數組指定複製一個新數組?

 

 

     

public class Practice{   

 

              

public static void main(String[] args){ 

 

                       

String[] a = {"a","d","e","w","f"}; 

 

                       

String[] b = new String[4]; 

 

                       

b = Arrays.copyOfRange(a, 2, 4); 

 

                     

System.out.println("b

數組的元素:

" + Arrays.asList(b)); 

 

            

 

     

 

    

運行結果爲:

 

 

               

b

數組的元素:

[e, w]; 

 

    

相信讀者已知道其中用法了吧,

type[] N = Arrays.copyofRange("

原數組

",i,j)

就是複製原數組賦值給新的數組

N,

指定從索引

i

j; 

 

九、

Arrays.deepToString()

用法,這個方法的用法同

Arrays.asList()

的用法相似

,

此方法是爲了將多維數組轉換

爲字符串而設計的;

 

 

    

下面通過例子來看一下它們的相同點。

 

 

    

例:輸出已知數組

a

中的元素。

 

 

   

public class Practice{   

 

         

public static void main(String[] args){ 

 

                

String[] a = {"a","d","e","w","f"}; 

 

                    

System.out.println("

Arrays.asList()

方法輸出:

" + Arrays.asList(a)); 

 

                    

System.out.println("

Arrays.deepToString()

方法輸出:

" + Arrays.deepToString(a));; 

 

            

 

   

 

    

運行結果爲:

 

 

              

Arrays.asList()

方法輸出:

[a, d, e, w, f]

 

 

            

Arrays.deepToString()

方法輸出:

[a, d, e, w, f]

 

 

十、

Arrays.deepEquals()

的用法。

 

 

    

說明:此方法與

 

Arrays.equals(Object[],Object[]) 

方法不同,此方法適用於任意深度的嵌套數組。

 

 

          

如果兩個數組引用均爲

 

null

,或者它們引用了包含相同元素數量的數組,並且兩個數組中的所

 

 

          

有相應元素對都是深層相等的,則認爲這兩個數組引用是深層相等的。

 

 

    

例:已知三個三維數組間的比較。

 

 

        

public class Practice{   

 

      

public static void main(String[] args){ 

 

                 

String[][][] d = new String[2][2][2]; 

 

                 

d[1][1][1] = "a"; 

 

                 

String[][][] e = new String[3][3][3]; 

 

                   

e[1][1][1] = "a"; 

 

                    

String[][][] f = new String[2][2][2]; 

 

                  

f[1][1][1] = "a"; 

 

                    

String[][][] g = new String[2][2][2]; 

 

                

g[1][1][1] = "ab"; 

 

    

 

                  

System.out.println("--------------------------"); 

 

                  

System.out.println("

輸出

equals()

方法與

deepEquals()

方法的區別;

"); 

 

                    

System.out.println("

數組

d

與數組

f

進行比較:

 

" + d.equals(f)); 

 

                  

System.out.println("

數組

d

與數組

f

進行比較:

 

" + Arrays.deepEquals(d, f)); 

 

                    

System.out.println("--------------------------"); 

 

    

 

                 

//

下面輸出比較結果

 

 

              

System.out.println("================================="); 

 

                

System.out.println("

數組

d

與數組

e

進行比較:

 

" + Arrays.deepEquals(d, e)); 

 

                

System.out.println("

數組

d

與數組

g

進行比較:

 

" + Arrays.deepEquals(d, g));  

 

                  

System.out.println("================================="); 

 

         

 

 

   

運行結果爲:

 

 

             

-------------------------- 

 

   

輸出

equals()

方法與

deepEquals()

方法的區別;

 

 

   

數組

d

與數組

f

進行比較:

 

false 

 

   

數組

d

與數組

f

進行比較:

 

true 

 

   

-------------------------- 

 

   

================================= 

 

  

數組

d

與數組

e

進行比較:

 

false 

 

   

數組

d

與數組

g

進行比較:

 

false 

 

   

================================= 

 

十一、

Collections.reverseOrder()

用法

(

反轉自然的順序

)

 

 

      

例:用除了循環外的另一方式逆向輸出已知數組

a

 

 

         

public class Practice{   

 

              

public static void main(String[] args){ 

 

                       

String[] a = {"a","b","c"}; 

 

                       

Arrays.sort(a,Collections.reverseOrder()); 

 

                       

System.out.println(Arrays.asList(a));    

 

                

 

        

 

      

運行結果爲:

[c, b, a];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章