天天看點

數組(Arrays和多元數組)

弄這個數組的原因是對多元數組的操作不是很明白,索性就弄一個整個數組的總結,反正java程式設計思想裡有這麼一個小專題,就用自己的五步法弄一個小結

現在的學習總結均以實用為第一要旨,直接用實踐來說明,了解理論為第二要旨,畢竟我現在的任務是先會用它,再談其他

數組的實踐裡面包含了對象數組,這有益于了解對象,和習慣對象引用的使用。

數組在java程式設計思想中是這樣定義:數組是最高效的存儲和随機通路對象引用序列的方式

恩,它也隻剩下性能高效這一個優點,現在集合的選擇使用是要優先于數組,誰叫集合比數組好用呢,調用一個方法即可

本來數組還有類型的優勢,泛型檢查,參數化類型讓它蕩然無存

本來數組還有基本資料類型的優勢,自動裝箱機制又讓它蕩然無存

說起來也挺可憐,傷口撒鹽,我優先選擇集合

主要内容是

      1、對象數組和基本類型數組的差別和聯系;

      2、Arrays類static方法System.arraycopy方法的使用;

      3、多元數組的使用和了解;

      4、資料和集合之間的互相轉換

User類

package ForArrays;

public class User {
	private int id;
	private String name;
	private String phone;
	public User(){
		
	}
	public User(int id, String name, String phone){
		this.id  = id;
		this.name = name;
		this.phone = phone;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}

}
           

TestArrayFor2D類

package ForArrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestArrayFor2D {

	public static void main(String[] args) {
		//對象數組與基本類型數組(包含String)的對别
		User[] users = new User[3];
		users[0] = new User(1,"ooo","password");
		users[1] = new User(2,"uuu","password");
		users[2] = new User();
		System.out.println(users);
		System.out.println(users.toString());
		System.out.println(Arrays.toString(users));
		System.out.println(users[0]);
		System.out.println(users[0].toString());
		System.out.println(users[0].getName());
		
		String[] str = {"ooo","ooo"};
		System.out.println(str);
		System.out.println(str.toString());
		System.out.println(Arrays.toString(str));
		System.out.println(str[0]);
		
		int[] a = {3,2,1};
		System.out.println(a);
		System.out.println(a[0]);
		System.out.println(Arrays.toString(a));
		
		//foreach循環
		for(User u : users){
			System.out.print(u.getName()+" ");
		}
		
		System.out.println();
		int sum = 0;
		for(int i : a){
			sum += i;
		}
		System.out.println(sum);
		
		//System.arraycopy()方法的使用,arraycopy()方法不支援自動裝箱機制,兩個數組需要相同的資料類型
		int[] b = {9,9,9,9,9,9,9,9};
		System.arraycopy(a, 0, b, 0, a.length);
		System.out.println(Arrays.toString(b));
		
		//Arrays類static方法的使用,toString()已用
		//Arrays.equals(),比較的是内容,不管對象的引用是否指向相同的對象位址
		String s = "ooo";
		String[] str2 = new String[2];
		str2[0] = s;
		str2[1] = s;
		System.out.println(Arrays.equals(a, b));
		System.out.println(Arrays.equals(str, str2));
		
		//Arrays.fill()一個雞肋的方法
		int[] c = new int[8];
		Arrays.fill(c, 8);
		System.out.println(Arrays.toString(c));
		
		//Arrays.sort()
		Arrays.sort(a);
		System.out.println(Arrays.toString(a));
		
		//Arrays.binarySearch()得是排好序的數組
		int val = Arrays.binarySearch(a, 1);
		System.out.println(val);
		
		
		//多元數組,二維為例,二維數組需得從高維到低維
		int [][] intArray = {{1},{2,2},{3,3,3}};
		
		for(int i = 0; i < intArray.length; i++){
			for(int j = 0; j < intArray[i].length; j++){
				System.out.print(intArray[i][j]+"  ");
			}
			System.out.println();
		}
		
		System.out.println(intArray.length+"  "+intArray[0].length);
		
		int [][] intArray2 = new int[2][];
		intArray2[0] = new int[2];
		intArray2[1] = new int[4];
		
		intArray2[0][0] = 1;
		intArray2[0][1] = 1;
		intArray2[1][0] = 2;
		intArray2[1][1] = 2;
		intArray2[1][2] = 2;
		intArray2[1][3] = 2;
		//Arrays.deepToString()在多元數組中使用
		System.out.println(Arrays.deepToString(intArray2));
		
		//三維數組,第一維裡有三個數組,第二維都隻包含一個數組(可以更複雜,包含多個),第三維裡面的數組元素個數不相同
		int [][][] array3D = { {{2,2}},{{1}},{{3,3,3}}};
		  for(int[][] i : array3D){
			  for(int[] j : i){
				  for(int k : j){
					  System.out.print(k+" ");
				  }
				  System.out.println();
			  }
		  }
		
		//數組和集合的互相轉換,asList()和toArray()括号裡都是數組變量名。基本資料類型要互相轉換需得使用它們的封裝類,因為集合儲存的是對象的引用
		//數組到集合
		String[] str3 ={"ooo","uuu"};
		List<String> list1 = new ArrayList<String>();
		list1 = Arrays.asList(str3);
		System.out.println(list1.get(0));
		
		int[] d = {1,1};
		List<int[]>  list2 = Arrays.asList(d);
		System.out.println(list2.get(0));
		
		Integer[] e = {1,1};
		List<Integer> list3 = Arrays.asList(e);
		System.out.println(list3.get(0));
		
		//集合到數組
		List<String> list4 = new ArrayList<String>();
		list4.add("yyy");
		list4.add("lll");
		//是否建立的不同用法
		String [] str4 = new String[list4.size()]; 
		str4 = list4.toArray(str4);
		//String[] str4 = list4.toArray(new String[0]);
		System.out.println(str4[0]);
		
		List<Integer> list5 = new ArrayList<Integer>();
		list5.add(9);
		list5.add(9);
		Integer[] f = new Integer [list5.size()];
		f = list5.toArray(f);
		System.out.println(f[0]);
	}

}
           

輸出:

[LForArrays.User;@15db9742

[LForArrays.User;@15db9742

[ForArrays.[email protected], [email protected], [email protected]]

[email protected]

[email protected]

ooo

[Ljava.lang.String;@70dea4e

[Ljava.lang.String;@70dea4e

[ooo, ooo]

ooo

[[email protected]

3

[3, 2, 1]

ooo uuu null 

6

[3, 2, 1, 9, 9, 9, 9, 9]

false

true

[8, 8, 8, 8, 8, 8, 8, 8]

[1, 2, 3]

1  

2  2  

3  3  3  

3  1

[[1, 1], [2, 2, 2, 2]]

2 2 

3 3 3 

ooo

[[email protected]

1

yyy

9

---------------------over-----------------------