版权
5.2一维数组
5.2.1创建数组
1.先声明,再用new关键字进行内存分配,声明有一下两种方式
数组元素类型 数组名字[ ];
数组元素类型[ ] 数组名字;
声明一维数组,代码如下:int arr[ ];
为数组分配内存,代码如下:arr = new int[5];
2.声明的同时为数组分配内存:
int month[] = new int[12]
5.2.2初始化一维数组
int arr[] = new int[]{1,2,3,4,5,25};
int arr2[] = {34,23,12,6}
5.2.3使用一维数组
例题5.1
package 课本;
public class 例p74 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int day[] = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 0;i < day.length;i++) {
System.out.println((i + 1) + "月有" + day[i] + "天");
}
}
}
5.3二维数组
5.3.1创建二维数组
1.先声明,再用new关键字进行内存分配
声明: 数组元素的类型 数组名字[ ][ ];
数组元素的类型[ ][ ] 数组名字;
声明数组
int a[ ][ ];
分配空间:
a = new int[2][4]
声明的同时进行分配:
int a = new int[2][4]
5.3.2初始化二维数组
type arrayname[][] = {value1,value2...valuen};
代码如下: int myarr[][] = {{12,0},{45,10}};
还可以直接给指定赋值如: a[1][1] = 20;
5.3.3使用二维数组
例5.2
package 课本;
public class 例p77 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[3][4];
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("");
}
}
}
5.4数组的基本操作
5.4.1遍历数组
例题5.3
package 课本;
public class 例p78上 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int b[][] = new int[][]{{1},{2,3},{4,5,6}};
for(int k = 0;k < b.length;k++) {
for(int c = 0;c < b[k].length;c++) {
System.out.print(b[k][c]);
}
System.out.println();
}
}
}
例题5.4
package 课本;
public class 例p78下 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr2[][] = {{4,3},{1,2}};
System.out.println("数组中的元素是:");
int i = 0;
for(int x[] : arr2) {
i++;
int j = 0;
for(int e : x) {
j++;
if(i == arr2.length && j == x.length) {
System.out.print(e);
}else
System.out.print(e + "、");
}
}
}
}
5.4.2填充替换数组元素
例题5.5
package 课本;
import java.util.Arrays;
public class 例p79 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[5];
Arrays.fill(arr, 8);
for(int i = 0;i < arr.length;i++) {
System.out.println("第" + i + "个元素是: " + arr[i]);
}
}
}
例题5.6
package 课本;
import java.util.Arrays;
public class 例p80上 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] {45,12,2,10};
Arrays.fill(arr,1,2,8);
for(int i = 0;i < arr.length;i++) {
System.out.println("第" + i + "个元素是: " + arr[i]);
}
}
}
填充索引前改后不改
5.4.3对数组进行排序
例题5.7
package 课本;
import java.util.Arrays;
public class 例p80下 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] {23,42,12,8};
Arrays.sort(arr);
for(int i = 0;i < arr.length;i++) {
System.out.println(arr[i]);
}
}
}
5.4.4复制数组
1.copyOf方法
语法如下:
copyOf(arr,int newiength)
如果新数组的长度大于数组arr的长度,则用0填充,溢出去掉。
例题5.8
package 课本;
import java.util.Arrays;
public class 例p81 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] {23,42,12};
int newarr[] = Arrays.copyOf(arr,5);
for(int i = 0;i < newarr.length;i++) {
System.out.println(newarr[i]);
}
}
}
copyOfRange()方法:
语法如下:
copyOfRange(arr,int formlndex,int tolndex)
例题5.9
package 课本;
import java.util.Arrays;
public class 例p82 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] {23,42,12,84,10};
int newarr[] = Arrays.copyOfRange(arr,0,3);
for(int i = 0;i < newarr.length;i++) {
System.out.println(newarr[i]);
}
}
}
5.4.5查询数组
例题5.10
packge 课本;
import java.util.Arrays;
public class 例p83 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int ia[] = new int[] {1,8,9,4,5};
Arrays.sort(ia);
int index = Arrays.binarySearch(ia,4);
System.out.println("4的索引位置是:" + index);
}
}
先排序,再查找
例题5.11
package 课本;
import java.util.Arrays;
public class 例p84 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str[] = new String[] {"ab","cd","ef","yz"};
int index = Arrays.binarySearch(str,0,2,"cd");
System.out.println("cd的索引位置是: " + index);
}
}
5.5数组排序算法
5.5.1冒泡排序
例题5.12
package 课本;
public class BubbleSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {63,4,24,1,3,15};
BubbleSort sorter = new BubbleSort();
sorter.sort(array);
}
public void sort(int[] array) {
for(int i = 1;i < array.length;i++) {
for(int j = 0;j < array.length-i;j++) {
if(array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
showArray(array);
}
public void showArray(int[] array) {
for(int i:array) {
System.out.print(">" + i);
}
System.out.println();
}
}
5.5.2直接选择排序
例题5.13
package 课本;
public class SelectSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {63,4,24,1,3,15};
SelectSort sorter = new SelectSort();
sorter.sort(array);
}
public void sort(int[] array) {
int index;
for(int i = 1;i < array.length;i++) {
index = 0;
for(int j = 1;j <= array.length - i;j++) {
if(array[j] > array[index]) {
index = j;
}
}
int temp = array[array.length - i];
array[array.length - i] = array[index];
array[index] = temp;
}
showArray(array);
}
public void showArray(int[] array) {
for(int i:array) {
System.out.print(">" + i);
}
System.out.println();
}
}
5.5.3反转排序
例题5.14
package 课本;
public class ReverseSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {10,20,30,40,50,60};
ReverseSort sorter = new ReverseSort();
sorter.sort(array);
}
public void sort(int[] array) {
System.out.println("数组原有内容:");
showArray(array);
int temp;
int len = array.length;
for(int i = 0;i < len/2;i++) {
temp = array[i];
array[i] = array[len -1 -i];
array[len -1 -i] = temp;
}
System.out.println("数组反转内容:");
showArray(array);
}
public void showArray(int[] array) {
for(int i : array) {
System.out.print("\t" + i);
}
System.out.println();
}
}