一维数组
声明方式:
type var[] 或 type[] var; 例子:int a[ ] , int[ ] a;
int[] s ; s = new int[5] ;
People p[ ]; p = new people[10];
public class Study {
// public static void main(String[] args) {
// int a[] = new int[5];
// People p[];
// p = new People[10];
// }
//
// class People {
//
// }
public static void main(String[] args) {
int[] a;
a = new int[5];
for(int i=0; i < 5; i ++){
a[i] = i;
}
for(int i=0; i < 5; i ++){
System.out.println(a[i]);
}
}
}
初始化:
2.静态初始化:在 定义数字的同时就为数组元素分配空间并赋值;
3.默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
public class Study {
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
Date d[] = new Date[3];
d[0] = new Date(2008,4,5);
d[1] = new Date(2008,4,5);
d[2] = new Date(2008,4,5);
}
}
class Date {
int year, month,day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
2、静态代码示例
public class Study {
public static void main(String[] args) {
Date d[] = {new Date(2012, 12, 12),new Date(2012, 12, 12),new Date(2012, 12, 12)};
}
}
class Date {
int year, month,day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
3、默认初始化
public class Study {
public static void main(String args[]) {
int a[] = new int[5];
System.out.println("" + a[3]);
}
}
二维数组的定义
声明方式:
type arrayName[][]; 例子:
int intArray[][]; 如:
int a[][]={{2,3},{1,5},{3,4}}; 定义了一个3×2的数组,并对每个元素赋值。
初始化:
2.静态初始化:在 定义数字的同时就为数组元素分配空间并赋值;
3.默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
public class Study {
public static void main(String args[]) {
int a[][] = {{1, 2}, {3, 4},{5, 6}};
for(int i = 0; i<a.length; i++){
for(int j = 0; j<a[i].length; j++){
System.out.println(a[i][j]);
}
}
}
}
public class Study {
public static void main(String args[]) {
int b[][] = {{1, 2}, {3, 4, 5},{5, 6, 7, 8}};
for(int i = 0; i<b.length; i++){
for(int j = 0; j<b[i].length; j++){
System.out.println(b[i][j]);
}
}
}
}
数组排序
1、利用Arrays带有的排序方法快速排序
public class Study {
public static void main(String[] args) {
int[] a = { 5, 4, 2, 4, 9, 1 };
Arrays.sort(a);
for (int i : a) {
System.out.println(i);
}
}
}
2、冒泡排序
import java.util.Arrays;
public class Study {
public static void main(String[] args) {
int[] a = { 5, 4, 2, 4, 9, 1 };
bubbleSort(a);
for (int i : a) {
System.out.println(i);
}
}
public static int[] bubbleSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if(a[i] > a[j]){
int temp;
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
return a;
}
}
3、选择排序算法
我们主要介绍简单选择排序、树型选择排序和堆排序。
简单排序:在简单选择排序过程中,所需移动记录的次数比较少。最好情况下,即待排序记录初始状态就已经是正序排列了,则不需要移动记录。最坏情况下,即待排序记录初始状态是按逆序排列的,则需要移动记录的次数最多为3(n-1)。简单选择过程中需要进行的比较次数与初始状态下待排序的记录序列的排列情况无关。当i=1时,需进行n-1次比较;当i=2时,需进行n-2次比较;依次类推,共需要进行的比较次数是∑ =(n-1)+(n-2)+…+2+1=n(n-1)/2,即进行比较操作的时间复杂度为O(n2)。这种方法其实是对冒泡排序的深入。
public class Study {
public static void main(String[] args) {
int[] a = { 5, 4, 2, 4, 9, 1 };
selectSort(a);
for (int i : a) {
System.out.println(i);
}
}
public static int[] selectSort(int[] args) {
for (int i = 0; i < args.length - 1; i++) {
int min = i;
for (int j = i + 1; j < args.length; j++) {
if (args[min] > args[j]) {
min = j;
}
}
if (min != i) {
int temp = args[i];
args[i] = args[min];
args[min] = temp;
}
}
return args;
}
}
4、插入排序算法
包括:直接插入排序,二分插入排序(又称折半插入排序),链表插入排序,希尔排序(又称缩小增量排序)。public class Study {
public static void main(String[] args) {
int[] a = { 5, 4, 2, 4, 9, 1 };
insertSort(a);
for (int i : a) {
System.out.println(i);
}
}
public static int[] insertSort(int[] args) {// 插入排序算法
for (int i = 1; i < args.length; i++) {
for (int j = i; j > 0; j--) {
if (args[j] < args[j - 1]) {
int temp = args[j - 1];
args[j - 1] = args[j];
args[j] = temp;
} else
break;
}
}
return args;
}
}
折半插入排序
public class Study {
public static void main(String[] args) {
int[] a = { 5, 4, 2, 4, 9, 1 };
halfInsert(a);
for (int i : a) {
System.out.println(i);
}
}
public static int[] halfInsert(int[] R) {
for (int i = 1; i < R.length; i++) {// 从第二个元素开始,需要做n-1趟插入排序,第一个元素自成有序区
int temp = R[i];// 暂存
int low = 0;// 定义从第一个元素开始为有序区
int high = i - 1;// 有序区的元素从一个开始逐渐增加
// low和high分别指向有序区中的第一个和最后一个元素
while (low <= high) {// 寻找在有序区中插入的位置,最后使high<low,就找到插入的位置
// 为high+1;也是low的位置
int m = (low + high) / 2;// 有序区的中间元素
if (temp < R[m]) {// 如果比中间的元素小,则插入位置在低半区
high = m - 1;
} else
// 否则,插入位置在高半区
low = m + 1;
}
for (int j = i; j > low; j--) {// 把从low开始以后或high+1以后的元素向后移动,插入
R[j] = R[j - 1];// 移动元素
}
R[low] = temp;// 插入在合适的位置
}
return R;
}
}
二分查找:
int binarySearch(int[] a, int value){
int low = 0;
int high = a.length-1;
while(low <= high){
mid = (low+high)/2; //**
if (a[mid] == value)
return mid;
else if (a[mid] > value)
high = mid-1;
else
low = mid +1;
}
return -1;
}
5、快速排序算法
void quickSort(int[] a, int low, int high) {
p = get(a, low, high);
quickSort(a, low, p-1);
quickSort(a, p+1, high);
}
int get(int[] a, int low, int high){
compare = a[low];
while(low < high){ //无论如何置换, 被置换的都包含compare的值
while(low<high && a[high]>=compare)
high--;
//在 low<high 的情况下找到a[high]<compare并置换
temp = a[low];
a[low] = a[high];
a[high] = temp;
while(low<high && a[low]<=compare)
low++;
//在 low<high 的情况下找到a[low]>compare并置换
temp = a[low];
a[low] = a[high];
a[high] = temp;
}
return low; //while(low==hight)停止循环, 并返回枢轴位置
}
数组重要方法
一、填充数组:Arrays.fill()方法
public class Study {
public static void main(String[] args) {
int[] a = new int[5];
Arrays.fill(a, 1);
for (int i : a) {
System.out.println(i);
}
}
} 输出结果为:
用法2:接受4个参数
public class Study {
public static void main(String[] args) {
int[] a = new int[5];
Arrays.fill(a, 1);
Arrays.fill(a, 1, 3, 2);
for (int i : a) {
System.out.println(i);
}
}
} 结果:
二、复制数组:clone()方法
clone()方法,限制:全部复制,无法部分的复制。
public class Study {
public static void main(String[] args) {
int[] a = new int[5];
int[] b;
Arrays.fill(a, 1);
b = a.clone();
for (int i : b) {
System.out.println(i);
}
}
} 结果:
1
1
1
1
1
三、比较数组:Arrays.equala()方法
public class Person {
String firstname, lastname;
Boolean sex;
int age;
public Person(String firstname, String lastname, Boolean sex, Integer age) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.sex = sex;
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
实现Comparator,定义自定义比较器
import java.util.Comparator;
public class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person arg0, Person arg1) {
if (arg0.getAge() > arg1.getAge()) {
return -1;
}
return 1;
}
}
测试比较器
public class Study {
public static void main(String[] args) {
Person[] p = {
new Person("ouyang", "feng", Boolean.TRUE, 27),
new Person("zhuang", "gw", Boolean.TRUE, 26),
new Person("zhuang", "gw", Boolean.FALSE, 28),
new Person("zhuang", "gw", Boolean.FALSE, 24)};
Arrays.sort(p, new PersonComparator());
for (Person person : p) {
System.out.println(person.getFirstname());
System.out.println(person.getLastname());
System.out.println(person.getAge());
System.out.println(person.getSex());
System.out.println();
}
}
}
四、数组排序:Arrays.sort()方法
五、查找数组:Arrays.binarySearch()方法
public class Study {
public static void main(String[] args) {
int[] a = {1, 2, 4, 5, 3};
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a, 3));
}
}
六、显示数组数据
public class Study {
public static void main(String[] args) {
int[] a = {1, 2, 4, 5, 3, 1};
System.out.println(Arrays.toString(a));
for (int i : a) {
System.out.println(i);
}
}
}
本文详细介绍了Java中一维数组和二维数组的声明、初始化方法及数组排序等操作,并提供了丰富的示例代码。
1214





