程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java中對Array數組的常用操作,javaarray數組

Java中對Array數組的常用操作,javaarray數組

編輯:JAVA綜合教程

Java中對Array數組的常用操作,javaarray數組


目錄:

備注:文內代碼具有關聯性。

1.聲明數組;

String [] arr;
int arr1[];
String[] array=new String[5];
int score[]=new int[3];

 

2.初始化數組;

//靜態初始化
int arr2[]=new int[]{1,2,3,4,5};
String[] array1={"馬超","馬雲","關羽","劉備","張飛"};
String[] array2=new String[]{"黃渤","張藝興","孫紅雷","小豬","牙哥","黃磊"};
int score[]=new int[3];
//動態初始化
for(int i=0;i<score.length;i++)
{    
  score[i]=i+1;  
}

 

 

3.查看數組長度;

int length=array1.length;
System.out.println("length:  "+array1.length);

 

4.遍歷數組;

for (int i = 0; i < array1.length; i++) {
	System.out.println(array1[i]);
}

 

5.int數組轉成string數組;

int[]  array3={1,2,3,4,5,6,7,8,9,0};
 		String arrStrings=Arrays.toString(array3);
 		System.out.println(arrStrings);

 

 6.從array中創建arraylist;

	ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(array1));
 	System.out.println(arrayList);

 

7.數組中是否包含某一個值;

String a="馬超";
String[] array1={"馬超","馬雲","關羽","劉備","張飛"};
if (Arrays.asList(array1).contains(a)) {
    System.out.println("馬超在這裡");
}

 

8.將數組轉成set集合;

String[] array2=new String[]{"黃渤","張藝興","孫紅雷","小豬","牙哥","黃磊"};
	Set<String> set=new HashSet<String>(Arrays.asList(array2));
	System.out.println(set);

 

9.將數組轉成list集合;

String[] array2=new String[]{"黃渤","張藝興","孫紅雷","小豬","牙哥","黃磊"};
//方法 1.
List<String> list=new ArrayList<String>();
for (int i = 0; i < array2.length; i++) {
    list.add(array2[i]);
}

String[] arrStrings2={"1","2","3"}; 
//方法 2.
List<String > list2=java.util.Arrays.asList(arrStrings2);
System.out.println(list2);

 

10.Arrays.fill()填充數組;

int[] arr3=new int[5];
 		Arrays.fill(arr3, 10);	//將數組全部填充10
               //遍歷輸出
 		for (int i = 0; i < arr3.length; i++) {
		    System.out.println(arr3[i]);
		}

 

11.數組排序;

//方法 1.
int[] arr4 = {3, 7, 2, 1, 9};
Arrays.sort(arr4);         //.sort(int[] a)   放入數組名字
for (int i = 0; i < arr4.length; i++) {
	System.out.println(arr4[i]);
}
//方法 2. 		
int[] arr5 = {3, 7, 2, 1, 9,3,45,7,8,8,3,2,65,34,5};
Arrays.sort(arr5, 1, 4);  //.sort(a, fromIndex, toIndex)    從第幾個到第幾個之間的進行排序
for (int i = 0; i < arr5.length; i++) {
    	System.out.println(arr5[i]);
}

 

12.復制數組;

//方法 1.
int[] arr6 = {3, 7, 2, 1};
int[] arr7=Arrays.copyOf(arr6, 10);  //指定新數組的長度
//方法 2. 		
int[] arr8=Arrays.copyOfRange(arr6, 1, 3); //只復制從索引[1]到索引[3]之間的元素(不包括索引[3]的元素)
for (int i = 0; i < arr8.length; i++) {
    	System.out.println(arr8[i]);
}

 

13.比較兩個數組;

int[] arr9 = {1, 2, 3, 4,5,6,7,8,9,0};
 		boolean arr10=Arrays.equals(arr6, arr9);
 		System.out.println(arr10);

 

14.去重復;

int[] arr11 = {1, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
//利用set的特性
Set<Integer> set2=new HashSet<Integer>();
for (int i = 0; i < arr11.length; i++) {
	set2.add(arr11[i]);
}
System.out.println(set2);
int[] arr12 = new int[set2.size()];
int j=0;
for (Integer i:set2) {
	arr12[j++]=i;
}
System.out.println(Arrays.toString(arr12));

 

15.查詢數組中的最大值和最小值;

		int[] arr11 = {10, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
		 //計算最大值
		int max = arr11[0];
	    for (int i = 1; i < arr11.length; i++) {
	         if (arr11[i] > max) {
	        	 max = arr11[i];
	        }
	      }
	      System.out.println("Max is " + max);
	      //計算最小值
	      int min = arr11[0];
	      for (int i = 0; i < arr11.length; i++) {
			if (arr11[i]<min) {
				min = arr11[i];
			}
		}
	      System.out.println("Min is " + min);

 

附:完整代碼:

package MyTest01;
 
import java.util.*;
 
public class ArrayTest02 {
 
    public static void main(String[] args) {
        //聲明數組
        String [] arr;
        int arr1[];
         
        //初始化數組
        int arr2[]=new int[]{1,2,3,4,5};
        String[] array1={"馬超","馬雲","關羽","劉備","張飛"};
        String[] array2=new String[]{"黃渤","張藝興","孫紅雷","小豬","牙哥","黃磊"};
         
        String[] array=new String[5];
     
         
        //查看數組的長度
        int length=array1.length;
        System.out.println("length:  "+array1.length);
         
        //輸出數組
//      System.out.println(array1);     //結果:[Ljava.lang.String;@32f22097
        System.out.println("arr2:  "+Arrays.toString(arr2));
         
        //遍歷數組
        for (int i = 0; i < array1.length; i++) {
//          System.out.println(array1[i]);
        }
        //int數組轉成string數組
        int[]  array3={1,2,3,4,5,6,7,8,9,0};
        String arrStrings=Arrays.toString(array3);
//      System.out.println(arrStrings);
         
        //從array中創建arraylist
        ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(array1));
        System.out.println(arrayList);
         
        //數組中是否包含某一個值
        String a="馬超";
        if (Arrays.asList(array1).contains(a)) {
            System.out.println("馬超在這裡");
        }
         
        //將數組轉成set集合
        Set<String> set=new HashSet<String>(Arrays.asList(array2));
        System.out.println(set);
         
        //將數組轉成list集合
        List<String> list=new ArrayList<String>();
        for (int i = 0; i < array2.length; i++) {
            list.add(array2[i]);
        }
        String[] arrStrings2={"1","2","3"};
        List<String > list2=java.util.Arrays.asList(arrStrings2);
        System.out.println(list2);
         
         
        //Arrays.fill()填充數組
        int[] arr3=new int[5];
        Arrays.fill(arr3, 10);  //將數組全部填充10
        for (int i = 0; i < arr3.length; i++) {
            System.out.println(arr3[i]);
        }
         
        //數組排序
        int[] arr4 = {3, 7, 2, 1, 9};
        Arrays.sort(arr4);
        for (int i = 0; i < arr4.length; i++) {
            System.out.println(arr4[i]);
        }
         
        int[] arr5 = {3, 7, 2, 1, 9,3,45,7,8,8,3,2,65,34,5};
        Arrays.sort(arr5, 1, 4);  //從第幾個到第幾個之間的進行排序
        for (int i = 0; i < arr5.length; i++) {
            System.out.println(arr5[i]);
        }
         
         
        //復制數組
        int[] arr6 = {3, 7, 2, 1};
        int[] arr7=Arrays.copyOf(arr6, 10);  //指定新數組的長度
         
        int[] arr8=Arrays.copyOfRange(arr6, 1, 3); //只復制從索引[1]到索引[3]之間的元素(不包括索引[3]的元素)
        for (int i = 0; i < arr8.length; i++) {
            System.out.println(arr8[i]);
        }
         
        //比較兩個數組
        int[] arr9 = {1, 2, 3, 4,5,6,7,8,9,0};
        boolean arr10=Arrays.equals(arr6, arr9);
        System.out.println(arr10);
     
        //去重復
        //利用set的特性
        int[] arr11 = {1, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
        Set<Integer> set2=new HashSet<Integer>();
        for (int i = 0; i < arr11.length; i++) {
            set2.add(arr11[i]);
        }
            System.out.println(set2);
        int[] arr12 = new int[set2.size()];
        int j=0;
        for (Integer i:set2) {
            arr12[j++]=i;
        }
        System.out.println(Arrays.toString(arr12));
    }
 
}

 

 

package MyTest01;

public class ArrayTest03 {

	public static void main(String[] args) {
		
		int[] arr11 = {10, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
		 //計算最大值
		int max = arr11[0];
	    for (int i = 1; i < arr11.length; i++) {
	         if (arr11[i] > max) {
	        	 max = arr11[i];
	        }
	      }
	      System.out.println("Max is " + max);
	      //計算最小值
	      int min = arr11[0];
	      for (int i = 0; i < arr11.length; i++) {
			if (arr11[i]<min) {
				min = arr11[i];
			}
		}
	      System.out.println("Min is " + min);
	}

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved