接上一篇博文
java作为一门面向对象的语言,数组是一种特殊的对象。
数组在定义时,不能分配空间。只有定义完后,可以给数组分配空间。
在Java中,无论使用数组或容器,都有边界检查。如果越界操作就会得到一个RuntimeException异常。
public class Main
{
public static void main (String[] args)
{
// 静态初始化
int intArray[]={1,2,3,4};
String stringArray[]={"abc", "How", "you"};
// 动态初始化
int data[]; //声明数组时不规定长度
data = new int[5];
int []array=new int[6];
}
}
public class Main
{
public static void main (String[] args)
{
// 静态初始化
int abc[][]={{1,2},{2,3},{3,4,5}};
// 动态初始化
// 1) 直接为每一维分配空间,格式如下:
// int arrayName = new type[arrayLength1][arrayLength2];
int ab[][] = new int[2][3];
/* 2) 从最高维开始,分别为每一维分配空间:
arrayName = new type[arrayLength1][];
arrayName[0] = new type[arrayLength20];
arrayName[1] = new type[arrayLength21];
…
arrayName[arrayLength1-1] = new type[arrayLength2n];
例: */
int a[][] = new int[2][];
a[0] = new int[3];
a[1] = new int[5];
/* 对二维复合数据类型的数组,必须首先为最高维分配引用空间,然后再顺次为低维分配空间。
而且,必须为每个数组元素单独分配空间。
例如: */
String s[][] = new String[2][ ];
s[0]= new String[2]; //为最高维分配引用空间
s[1]= new String[2]; //为最高维分配引用空间
s[0][0]= new String("Good");// 为每个数组元素单独分配空间
s[0][1]= new String("Luck");// 为每个数组元素单独分配空间
s[1][0]= new String("to"); // 为每个数组元素单独分配空间
s[1][1]= new String("You"); // 为每个数组元素单独分配空间
}
}
public class Practice
{
public static void main (String[] args)
{
String[] A = {"H","e","l","l","o"};
String[] B = new String[3];
System.arraycopy(A, 0, B, 1, B.length-1);
for (int i = 0; i < B.length; i ++)
System.out.print(B[i] + " ");
}
}
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String[] A = new String[5];
Arrays.fill(A, "Hello");
for (int i=0;i
运行结果为:
Hello Hello Hello Hello Hello
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String[] A = {"1","2","3"};
String[] B = {"一","二","三"};
String[] C = {"1","2","3"};
System.out.println(Arrays.equals(A, B));
System.out.println(Arrays.equals(A, C));
}
}
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String[] A = {"H","e","l","l","o"};
System.out.println(Arrays.asList(A));
}
}
import java.util.*;
public class Practice implements Comparator
{
public static void main(String[] args)
{
String[] A = {"A","B","c","D","e","f","G","H"};
Arrays.sort(A,new Practice());
System.out.println(Arrays.asList(A));
}
public int compare (Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
}
运行结果为:
[A, B, c, D, e, f, G,H]
import java.util.*;
public class Practice
{
public static void main (String[] args)
{
String[] a = {"a","d","e","w","f"};
Arrays.sort(a);
System.out.println(Arrays.asList(a));
int index1 = Arrays.binarySearch(a, "f");
System.out.println("要查找的值存在的时候:" + index1);
int index2 = Arrays.binarySearch(a, "n");
System.out.println(index2);
index2 = -index2 - 1;
System.out.print("当不存在的时候输出该值最可能存在的位置:" + index2);
}
}
import java.util.*;
public class Practice
{
public static void main (String[] args)
{
String[] a = {"a","d","e","w","f"};
String[] b = new String[4];
String[] c = new String[5];
String[] d = new String[6];
b = Arrays.copyOf(a, b.length);
c = Arrays.copyOf(a, c.length);
d = Arrays.copyOf(a, d.length);
System.out.println("b数组的元素:" + Arrays.asList(b));
System.out.println("c数组的元素:" + Arrays.asList(c));
System.out.println("d数组的元素:" + Arrays.asList(d));
}
}
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String[] a = {"a","d","e","w","f"};
String[] b = new String[4];
b = Arrays.copyOfRange(a, 2, 4);
System.out.println("b数组的元素:" + Arrays.asList(b));
}
}
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String[] a = {"a","d","e","w","f"};
System.out.println("用Arrays.asList()方法输出:" + Arrays.asList(a));
System.out.println("用Arrays.deepToString()方法输出:" + Arrays.deepToString(a));
}
}
import java.util.*;
public class Practice
{
public static void main (String[] args)
{
String[][][] d = new String[2][2][2];
d[1][1][1] = "a";
String[][][] e = new String[3][3][3];
e[1][1][1] = "a";
String[][][] f = new String[2][2][2];
f[1][1][1] = "a";
String[][][] g = new String[2][2][2];
g[1][1][1] = "ab";
System.out.println("--------------------------");
System.out.println("输出equals()方法与deepEquals()方法的区别;");
System.out.println("数组d与数组f进行比较: " + d.equals(f));
System.out.println("数组d与数组f进行比较: " + Arrays.deepEquals(d, f));
System.out.println("--------------------------");
//下面输出比较结果
System.out.println("=================================");
System.out.println("数组d与数组e进行比较: " + Arrays.deepEquals(d, e));
System.out.println("数组d与数组g进行比较: " + Arrays.deepEquals(d, g));
System.out.println("=================================");
}
}
import java.util.*;
public class Practice
{
public static void main (String[] args)
{
String[] a = {"a","b","c"};
Arrays.sort(a,Collections.reverseOrder());
System.out.println(Arrays.asList(a));
}
}
运行结果为:
[c, b, a]
import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
class Node implements Comparable
{
public int val;
public Node(int v) {this.val=v;}
public int compareTo (Object obj)
{
//instanceof是Java的一个二元操作符它的作用是判断其左边对象是否为其右边类的实例,返回boolean类型的数据
if (obj instanceof Node)
{
Node tmp=(Node)obj;
if (this.valtmp.val)
return 1;
}
return 0;
}
}
public class Main
{
public static void main(String args[]) throws Exception
{
int i;
Scanner cin=new Scanner(System.in);
Node a[]=new Node[4];
for (i=0;i<4;i++)
a[i]=new Node(i+1);
Arrays.sort(a);
for (i=0;i<4;i++)
System.out.print(a[i].val+((i==3)?"\n":" "));
}
}
import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
class stack
{
private int a[]=new int[1001],top;
public stack() {top=0;}
public void push(int val) {a[top++]=val;}
public boolean empty() {return top==0;}
public int pop ()
{
if (top==0)
{
System.out.println("警告:栈为空!");
return -1;
}
return this.a[--top];
}
}
public class Main
{
public static void main(String args[]) throws Exception
{
stack S=new stack();
S.push(5);
S.push(7);
S.push(98);
S.push(123);
while (!S.empty())
System.out.print(S.pop()+" ");
}
}
import java.util.*;
class Student
{
String name;
int age;
Student (String str, int tmp)
{
name = str;
age = tmp;
}
}
public class Main
{
public static void main(String[] args)
{
Student zlj = new Student("丁晓宇", 21);
Student dxy = new Student("赵四", 22);
Student cjc = new Student("张三", 11);
Student lgc = new Student("刘武", 19);
List studentList = new ArrayList();
studentList.add(zlj);
studentList.add(dxy);
studentList.add(cjc);
studentList.add(lgc);
Collections.sort(studentList, new SortByAge());
for (Student student : studentList)
System.out.println(student.name + " / " + student.age);
System.out.println(" = ");
Collections.sort(studentList, new SortByName());
for (Student student : studentList)
System.out.println(student.name + " / " + student.age);
}
}
class SortByAge implements Comparator
{
public int compare (Object o1, Object o2)
{
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.age > s2.age)
return 1;
return 0;
}
}
class SortByName implements Comparator
{
public int compare (Object o1, Object o2)
{
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.name.compareTo(s2.name);
}
}