天天看点

数组(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-----------------------