Java数组基本使用

数组使用[]来访问元素;List使用get();set()方法



import java.util.*;

class BerylliumSphere {
	private static long counter;
	private final long id = counter++;
	public String toString(){return "Sphere "+id;}
}

public class ContainerComparison{
	public static void main(String[] args){
		BerylliumSphere[] spheres = new BerylliumSphere[10];
		for(int i=0;i<5;i++)
			spheres[i] = new BerylliumSphere();
		System.out.println(Arrays.toString(spheres));
		System.out.println(spheres[4]);
		List<BerylliumSphere> sphereList = new ArrayList<BerylliumSphere>();
		for(int i=0;i<5;i++)
			sphereList.add(new BerylliumSphere());
		System.out.println(sphereList);
		System.out.println(sphereList.get(4));
		//int [] integers = {0,1,2,3,4,5};
		List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5));
		intList.add(97);
		System.out.println(intList);
		System.out.println(intList.get(4));
	}
}

无论使用哪种类型数组,数组标识符只是一个引用,指向堆中创建的一个真实对象,这个对象用以保存指向其他对象的引用。

对象数组和基本类型数组在使用上几乎是相同的,唯一的区别就是对象数字保存的是引用,基本数组保存的是基本类型的值。

package com.aijie.test;

import java.util.Arrays;

public class ArrayOptions {
	/**
	 * length是数组的大小,而不是实际保存的元素个数  新生成一个数字对象时,其中所有的引用被自动初始化为null;
	 * 检查其中的引用是否为null,即可知道数组的某个位置是否有对象。同样,基本数据类型的如果是数值型的,就被自动初始化
	 * 为0;如果是字符型的(char),就被自动初始为(char)O;如果是布尔型(boolean),就被自动初始为false.
	 * @param args
	 */
	public static void main(String[] args) {
		BerylliumSphere[] a;
		BerylliumSphere[] b = new BerylliumSphere[5];
		System.out.println("b: "+ Arrays.toString(b));
		BerylliumSphere[] c = new BerylliumSphere[4];
		for(int i=0;i<c.length;i++)
			if(c[i]==null)
				c[i] = new BerylliumSphere();
		
		BerylliumSphere[] d = {new BerylliumSphere(),new BerylliumSphere(),new BerylliumSphere(),new BerylliumSphere()};
		a = new BerylliumSphere[]{new BerylliumSphere(),new BerylliumSphere()};
		System.out.println("a.length:"+a.length);
		System.out.println("b.length:"+b.length);
		System.out.println("c.length:"+c.length);
		System.out.println("d.length:"+d.length);
		a = d;
		System.out.println("a.length:"+a.length);
		
		
		int[] e;
		int[] f = new int[5];
		System.out.println("f:"+Arrays.toString(f));
		int[] g = new int[4];
		for(int i=0;i<g.length;i++)
			g[i] = i*i;
		int[] h = {11,47,93};
		System.out.println("f.length = "+f.length);
		System.out.println("g.length = "+g.length);
		System.out.println("h.length = "+h.length);
		e = h;
		System.out.println("e.length = "+e.length);
		e = new int[]{1,2};
		System.out.println("e.length = "+e.length);		
	}

}

返回一个数组

package com.aijie.test;   

import java.util.Arrays;
import java.util.Random;

/**  
 * @author   E-mail:   
 * @version 2018年5月28日 下午10:10:54  
 */
public class IceCream {
	
	private static Random rand = new Random(47);
	static final String[] FLAVORS = {
		"Chocolate","Strawberry","Vanilla Fudge Swirl",
		"Mint Chip","Mocha Almond Fudge","Rum Raisin",
		"Praline Cream","Mud Pie"
	};
	
	public static String[] flavorSet(int n){
		if(n>FLAVORS.length)
			throw new IllegalArgumentException("Set too big");
		String[] results = new String[n];
		boolean[] picked = new boolean[FLAVORS.length];
		for(int i=0;i<n;i++){
			int t;
			do
				t = rand.nextInt(FLAVORS.length);
			while(picked[t]);
			results[i] = FLAVORS[t];
			picked[t] = true;
		}
		return results;
			
	}
	public static void main(String[] args) {
		for(int i=0;i<7;i++)
			System.out.println(Arrays.toString(flavorSet(3)));
	}
}
 
[Rum Raisin, Mint Chip, Mocha Almond Fudge]
[Chocolate, Strawberry, Mocha Almond Fudge]
[Strawberry, Mint Chip, Mocha Almond Fudge]
[Rum Raisin, Vanilla Fudge Swirl, Mud Pie]
[Vanilla Fudge Swirl, Chocolate, Mocha Almond Fudge]
[Praline Cream, Strawberry, Mocha Almond Fudge]
[Mocha Almond Fudge, Strawberry, Mint Chip]

多维数组;打印多维数组使用Arrays.deepToString().

数组中构成矩阵的每个向量都可以具有任意的长度(这被称为粗糙数组)

package com.aijie.test;   

import java.util.Arrays;
import java.util.Random;

/**  
 * @author   E-mail:   
 * @version 2018年5月29日 上午7:26:36  
 */
public class RaggedArray {

	public static void main(String[] args) {
		/**
		 * 数组中构成矩阵的每个向量都可以具有任意的长度(这被称为粗糙数组).
		 * 第一个new创建了数组对象,其第一维的长度由随机数确认,其他维的长度则没有定义
		 * 位于for循环内第二个new 则会决定第二维的长度。知道碰到第三个new,第三维
		 * 的长度才得以确认。
		 */
		Random rand = new Random(47);
		
		int[][][] a = new int[rand.nextInt(7)][][];
		for(int i=0;i<a.length;i++){
			a[i] = new int[rand.nextInt(5)][];
			for(int j=0;j<a[i].length;j++)
				a[i][j]=new int[rand.nextInt(5)];
		}
		System.out.println(Arrays.deepToString(a));
	}

}
 

使用Arrays.fill()填充数组

package com.aijie.test;   

import java.util.Arrays;

/**  
 * @author   E-mail:   
 * @version 2018年5月29日 上午8:13:12  
 */
public class FillingArrays {

	public static void main(String[] args) {
		int size = 6;
		String[] a9 = new String[size];
		Arrays.fill(a9, "Hello");
		System.out.println("a9:"+Arrays.toString(a9));
		Arrays.fill(a9, 3, 5, "World");
		System.out.println("a9:"+Arrays.toString(a9));
	}

}
 
package com.aijie.test;   

import java.util.Arrays;

/**  
 * @author   E-mail:   
 * @version 2018年5月29日 上午8:25:42  
 */
public class CopyingArrays {
	
	public static void main(String[] args) {
		/**
		 * System.arraycopy(i, 0, j, 0, i.length);
		 * i:源数组
		 * 源数组什么位置
		 * j:目标数组
		 * 目标数组位置
		 * i.length:复制的元素个数
		 * 
		 * 
		 * fill(i, 47);
		 * i:填充数组
		 * 
		 * fill(i,3, 5, 47);
		 * i:填充数组
		 * 3:起始位置
		 * 5:截止位置
		 */
		int[] i = new int[7];
		int[] j = new int[10];
		Arrays.fill(i, 47);
		Arrays.fill(j, 99);
		System.arraycopy(i, 0, j, 0, i.length);
		System.out.println(Arrays.toString(j));
		int[] k = new int[5];
		Arrays.fill(k, 103);
		System.arraycopy(i, 0, k, 0, k.length);
		System.out.println(Arrays.toString(k));
		Arrays.fill(k, 103);
		System.arraycopy(k, 0, i, 0, k.length);
		System.out.println(Arrays.toString(i));
		
		Integer[] u = new Integer[10];
		Integer[] v = new Integer[5];
		Arrays.fill(u, new Integer(47));
		Arrays.fill(v, new Integer(99));
		System.out.println(Arrays.toString(u));
		System.out.println(Arrays.toString(v));
		System.arraycopy(v, 0, u, u.length/2, v.length);
		System.out.println(Arrays.toString(u));

		/**
		 * 数字比较
		 * Arrays.equals(s1,s2);
		 * 数组相等条件是元素个数必须相等,并且对应位置的元素也相等。他可以通过对每一个元素使用equals()方法
		 */
	}

}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值