//數組的聲明和賦值的各種方式
public class ArrayTest
{
public static void main(String[] args)
{
/* 第一種方式,先聲明後賦值,這是java的語言規範標準
int[] a = new int[4];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
System.out.println(a[3]);
*/
/* 第二種方式,先聲明後賦值,這種方法是c/c++語言的規範
int a[] = new int[2];
a[0] = 1;
a[1] = 2;
System.out.println(a[1]);
*/
/* 第三種方式:直接賦值
int[] a = {1, 2, 3, 4};
System.out.println(a[2]);
int[] b = new int[]{1, 2, 3, 4};
System.out.println(b[3]);
*/
/* 第四種方式賦值:for循環賦值
int[] a = new int[100];
for(int i = 0; i < a.length; i++)
{
a[i] = i + 1;
}
for(int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
a.length = 200;//這是錯誤的,因爲數組int的修飾符是public final
*/
/*
int[] a = new int[4];
System.out.println(a[0]);//由於整型的成員變量的默認初始值0,數組是一種對象,它的成員變量默認初始值也是0;
boolean[] b = new boolean[3];//同上,這是個boolean數組的默認初始值爲false;
System.out.println(b[2]);
*/
//即使數組a和數組b的內容長度和數值都一樣,但是它們是不同的對象,所以引用地址是不一樣的,因此a.equals(b)的結果是false;
int[] a = {1, 2 ,3};
int[] b = {1, 2, 3};
System.out.println(a.equals(b));
}
}
//類和數組對象
public class ArrayTest2
{
public static void main(String[] args)
{
Person[] p = new Person[3];
p[0] = new Person(10);
p[1] = new Person(20);
p[2] = new Person(30);
for(int i = 0; i < p.length; i++)
{
System.out.println(p[i].age);
}
Person[] p2 = new Person[5];
for(int i = 0; i < p2.length; i++)
{
System.out.println(p2[i]);
}
}
}
class Person
{
int age;
public Person(int age)
{
this.age = age;
}
}
輸出結果:
10
20
30
Null
Null
Null
Null
Null
//數組對象中的屬性賦值
public class ArrayTest3
{
public static void main(String[] args)
{
Student[] s = new Student[100];
for(int i = 0; i < s.length; i++)
{
s[i] = new Student();
s[i].name = i % 2 == 0 ? "zhangsan" : "lisi";
}
for(int i = 0; i < s.length; i++)
{
System.out.println(s[i].name);
}
}
}
class Student
{
String name;
}
//介紹二維數組
public class ArrayTest4
{
public static void main(String[] args)
{
int[][] a = new int[2][3];
//System.out.println(i[0] instanceof int[]);//這表示i[0]的數組類型是int[]
int m = 0;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
m++;
a[i][j] = 2 * m;
}
}
}
}
//已知行數組明確,不明確列數組的前提下
public class ArrayTest5
{
public static void main(String[] args)
{
/*
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[1];
*/
//int[][] a = new int[][3];//這是錯誤的表示方式,因爲行數組不明確,即使知道列數組也沒用
//int[] a = new int[]{1, 2 ,3};
int[][] a = new int[][]{ {1 ,2 ,3}, {4}, {5, 6, 7, 8} };
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a[i].length; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
//接口與數組
public class ArrayTest6
{
public static void main(String[] args)
{
I[] i = new I[2];
i[0] = new C();
i[1] = new C();
int[] a = new int[]{1 , 2 ,3};
I[] b = new I[]{new C(), new C()};
}
}
interface I
{
}
class C implements I
{
}
//介紹三維數組
public class ThreeDimensionArrayTest
{
public static void main(String[] args)
{
int[][][] a = new int[2][3][4];
System.out.println(a instanceof int[][][]);
System.out.println(a[0] instanceof int[][]);
System.out.println(a[0][0] instanceof int[]);
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a[i].length; j++)
{
for(int k = 0; k < a[i][j].length; k++)
{
a[i][j][k] = 100;
}
}
}
}
}
//值交換的三種方式
public class Swap
{
public static void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
public static void main(String[] args)
{
int x = 3;
int y = 4;
Swap.swap(x, y);
System.out.println(x);
System.out.println(y);
System.out.println("-----------------");
int temp = x;
x = y;
y = temp;
System.out.println(x);
System.out.println(y);
System.out.println("-----------------");
int a = 3;
int b = 4;
a = a + b;
b = a - b;
a = a - b;
System.out.println(a);
System.out.println(b);
}
}
//數組傳遞起作用,值傳遞不起作用
public class Swap2
{
public static void swap(char[] ch, char c)
{
ch[0] = 'B';
c = 'D';
}
public static void main(String[] args)
{
char[] ch = {'A', 'C'};
swap(ch, ch[1]);
for(int i = 0; i < ch.length; i++)
{
System.out.println(ch[i]);
}
}
}
public class Swap3
{
public static void swap(int[] i)
{
int temp = i[0];
i[0] = i[1];
i[1] = temp;
}
public static void main(String[] args)
{
int[] i = {1, 2};
swap(i);
System.out.println(i[0]);
System.out.println(i[1]);
}
}
//數組的複製,調用的是lang包下的System類下的arraycopy方法
public class ArrayCopy
{
public static void main(String[] args)
{
int[] a = new int[]{1, 2, 3, 4};
int[] b = new int[4];
System.arraycopy(a, 0, b, 0, 4);
for(int i = 0; i < b.length; i++)
{
System.out.print(b[i] + " ");
}
}
}
//數組的比較,有自定義和系統自帶的
import java.util.Arrays;
public class ArrayEqualsTest
{
// compare the content of two arrays,這是自定義數組比較
public static boolean isEquals(int[] a, int[] b)
{
if(a == null || b == null)
{
return false;
}
if(a.length != b.length)
{
return false;
}
for(int i = 0; i < a.length; i++)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(isEquals(a, b));
System.out.println("----------");
//這是調用util包下的Arrays的方法,裏面定義了很多類型的數組比較,比較數組的內容是否一致,無須看引用地址是否一樣
System.out.println(Arrays.equals(a, b));
}
}