一维数组
- 数组赋值
- 数组翻转
- 数组扩容
- 数组缩减
- 数组排序(冒泡排序)
- 数组查找
- 数组扩容后添加元素(在最后)
二维数组
- 数组静态初始化
- 数组动态初始化
- 杨辉三角
数组初始化三种方式
数组动态初始化1
double[] a = new double[3];
数组动态初始化2
double[] a; //声明数组
a = new double[3]; // 分配空间
数组静态初始化(值有限)
double[] a = {3, 5, 1, 3.4, 2, 50};
两种打印a-z方式
public class CyclePractice{
public static void main (String[] args){
char[] apha = new char[26];
for (int i = 0 ;i < 26; i++){
apha[i] = (char)('a' + i);
System.out.print(apha[i]);
}
}
}
在这里插入代码片
public class CyclePractice{
public static void main (String[] args){
char[] apha = new char[26];
apha[0] = 'a';
System.out.print(apha[0]);
for (int i = 0 ;i < 25; i++){
apha[i+1] = (char)(apha[i] + 1);
System.out.print(apha[i+1]);
}
}
}
值传递和引用传递
值传递:简单把值给对方
引用传递: 把房子给对方
数组逆序(三种情况)
【只有arr1 不开辟新房子】仅仅输出逆序,数组实际没有逆序
public class CyclePractice{
public static void main (String[] args){
double[] arr1 = {3, 5, 1, 3.4, 2, 50};
for (int i = arr1.length - 1; i >= 0 ;i--){
System.out.println(arr1[i]);
}
}
}
【arr1 不开辟新房子】:原数组位置交换后的逆序
public class CyclePractice{
public static void main (String[] args){
double[] arr1 = {3, 5, 1, 3.4, 2, 50};
int len = arr1.length;
double temp = 0;
for (int i = 0; i < len/2 ;i++){
temp = arr1[len - 1 - i];
arr1[len - 1 - i] = arr1[i];
arr1[i] = temp;
}
}
【arr2 开辟新房子】:开辟了新数组,为原数组的逆序
public class CyclePractice{
public static void main (String[] args){
double[] arr1 = {3, 5, 1, 3.4, 2, 50};
double[] arr2 = new double[arr1.length];
for (int i = 0 ; i < arr1.length ;i++){
arr2[i] = arr1[arr1.length - 1 - i];
System.out.println(arr2[i]);
}
}
}
【arr2 开辟新房子】:开辟了新数组,原数组指向新数组
arr1 = arr2 ;
旧数组空间被JVM回收
数组扩容
思路,新建一个比原来大m的数组,把原数组数据放到新数组中,新数据添加到新数组中,旧数组指向新数组。
- 添加一个指定数据
public class ArrayAdd{
public static void main (String[] args){
double[] arr1 = {3, 5, 1, 3.4, 2, 50};
double[] arr2 = new double[arr1.length + 1];
for (int i = 0 ; i < arr1.length ;i++){
arr2[i] = arr1[i];
}
arr2[arr1.length] = 8;
arr1 = arr2;
for (int i = 0 ; i < arr1.length ;i++){
System.out.println(arr1[i]);
}
}
}
- 添加多个固定数据
import java.util.*;
public class ArrayAdd{
public static void main (String[] args){
Scanner scanner = new Scanner(System.in);
String in = scanner.next();
double[] arr1 = {3, 5, 1, 3.4, 2, 50};
do{
double[] arr2 = new double[arr1.length + 1];
for (int i = 0 ; i < arr1.length ;i++){
arr2[i] = arr1[i];
}
arr2[arr1.length] = 8; //添加固定数据8
arr1 = arr2;
for (int i = 0 ; i < arr1.length ;i++){
System.out.println(arr1[i]);
}
in = scanner.next();
}while("y".equals(in));
}
}
3. 添加多个用户输入数据
import java.util.*;
public class ArrayAdd{
public static void main (String[] args){
Scanner scanner = new Scanner(System.in);
String in = scanner.next();
double[] arr1 = {3, 5, 1, 3.4, 2, 50};
do{
double[] arr2 = new double[arr1.length + 1];
for (int i = 0 ; i < arr1.length ;i++){
arr2[i] = arr1[i];
}
double myAdd = scanner.nextDouble(); //接收用户输入数据
arr2[arr1.length] = myAdd; // 添加到arr2的最后一个位置
arr1 = arr2;
for (int i = 0 ; i < arr1.length ;i++){
System.out.println(arr1[i]);
}
in = scanner.next();
}while("y".equals(in));
}
}
4. 动态缩减数组
缩减一个
public class ArrayReduce{
public static void main (String[] args){
int[] arr = {1,2,3,4,5};
int len = arr.length;
int[] arr2 = new int[len - 1];
for (int i = 0 ; i < arr2.length ; i++){
arr2[i] = arr[i];
}
arr = arr2;
for (int i = 0 ; i < arr2.length ; i++){
System.out.println(arr[i]);
}
}
}
缩减到只有一个元素
import java.util.*;
public class ArrayReduce{
public static void main (String[] args){
Scanner scanner = new Scanner(System.in);
String in = "";
int[] arr = {1,2,3,4,5};
do{
int len = arr.length;
int[] arr2 = new int[len - 1];
for (int i = 0 ; i < arr2.length ; i++){
arr2[i] = arr[i];
}
arr = arr2;
for (int i = 0 ; i < arr.length ; i++){
System.out.println(arr[i]);
}
in = scanner.next();
}while("y".equals(in) && arr.length > 1);
//for (int i = 0 ; i < arr.length ; i++){
// System.out.println(arr[i]);
//}
}
}
- 排序
内部排序:加载到内存里(交换排序、选择排序、插入排序)
外部排序:数据量太大,需要额外存储空间(合并排序、直接合并排序)
冒泡排序,每一轮排序冒出一个最大值放最右边,每一轮排序比较次数依次减一
写法1:添加cycle循环变量,每次循环4,3,2,1次。
public class BubbleSort{
public static void main (String[] args){
int[] arr = {98,87,76,54,65,43,32,1};
int len = arr.length;
int temp = 0;
for (int cycle = len - 1 ; cycle > 0 ;cycle --){
for (int i = 1; i < cycle ;i++){
int front = i - 1;
int last = i;
if(arr[front] > arr[last]){
temp = arr[front];
arr[front] = arr[last];
arr[last] = temp;
}
}
for (int i = 0; i < len ;i++){
System.out.print(arr[i] + " ");
}
System.out.print("\n");
}
}
}
{98,87,76,54,65,43,32,1}
结果
87 76 54 65 43 32 1 98
76 54 65 43 32 1 87 98
54 65 43 32 1 76 87 98
54 43 32 1 65 76 87 98
43 32 1 54 65 76 87 98
32 1 43 54 65 76 87 98
1 32 43 54 65 76 87 98
写法2:
for (int cycle = 0 ; cycle < len - 1 ;cycle ++){
for (int i = 1; i < len - cycle ;i++){
int front = i - 1;
int last = i;
if(arr[front] > arr[last]){
temp = arr[front];
arr[front] = arr[last];
arr[last] = temp;
}
}
for (int i = 0; i < len ;i++){
System.out.print(arr[i] + " ");
}
System.out.print("\n");
}
87 76 54 65 43 32 1 98
76 54 65 43 32 1 87 98
54 65 43 32 1 76 87 98
54 43 32 1 65 76 87 98
43 32 1 54 65 76 87 98
32 1 43 54 65 76 87 98
1 32 43 54 65 76 87 98
优化:
待补充
- 查找
-
顺序查找
-
二分查找(待补充)
顺序查找
import java.util.*;
public class SeqSearch{
public static void main (String[] args){
String[] names = {"Tom0", "Tom1", "Tom2", "Tom3", "Tom4"};
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
int flag = -1;
for(int i = 0 ; i < names.length ; i++){
if(names[i].equals(input)){
System.out.print("We have found the name.");
flag = 1;
}
}
if (flag == -1){
System.out.print("We have not found the name.");
}
}
}
结果
D:\College\Javacode>java SeqSearch
Tom3
We have found the name.
D:\College\Javacode>java SeqSearch
Toms
We have not found the name.
二维数组
基本内容
数组静态初始化
public class TwoDemensionArray{
public static void main (String[] args){
int[][] arr = {{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15,16}};
for (int i = 0; i < arr.length ; i++){
for (int j = 0; j < arr[i].length ; j++){
System.out.print(arr[i][j] + "\t");
}
System.out.print("\n");
}
}
}
数组动态初始化
int[][] arr;
arr = new int[2][3];
int[][] arr = new int[2][3];
import java.util.*;
public class TwoDemensionArray{
public static void main (String[] args){
int[][] arr = new int[3][];
for (int i = 0; i < arr.length ; i++){
arr[i] = new int[i + 1];
for (int j = 0; j < arr[i].length ; j++){
arr[i][j] = i + 1;
}
for (int j = 0; j < arr[i].length ; j++){
System.out.print(arr[i][j] + "\t");
}
System.out.print("\n");
}
}
}
1
2 2
3 3 3
exercise :打印杨辉三角
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
观察,发现如果arr[x][0]恒为1,arr[x][x]恒为1,其他情况arr[m][n]的值是它脑袋上的+他脑袋上左边的数。
import java.util.*;
public class TwoDemensionArray{
public static void main (String[] args){
int[][] arr = new int[6][];
for (int i = 0; i < arr.length ; i++){
arr[i] = new int[i + 1];
for (int j = 0; j < arr[i].length ; j++){
if(j == 0 || i == j){
arr[i][j] = 1;
}else{
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
for (int j = 0; j < arr[i].length ; j++){
System.out.print(arr[i][j] + "\t");
}
System.out.print("\n");
}
}
}
之前没搞懂的点:此时,x是一维数组,y是二维数组
int[] x, y[];
布尔类型数组没有赋值时,默认为false
exercise:已知一个升序数组要求加入一个元素后,仍为升序=》
int[] arr = {10, 12, 23, 45, 90};
public class ArrayAddSort{
public static void main (String[] args){
int[] arr = {10, 12, 45, 90};
int add = 100;
int flag = -1;
int[] arr2 = new int[arr.length + 1];
for (int i = arr.length - 1 ; i >= 0 ; i--){
if(add > arr[i]){
arr2[i] = arr[i];
break;
}else{
arr2[i+1] = arr[i];
arr2[i] = add;
flag = 1;
}
}
//if add is bigger than all the nums in arr, then add
// it in the end.
if(flag == -1){
arr2[arr.length] = add;
}
arr = arr2;
for (int j = 0; j < arr.length ; j++){
System.out.print(arr[j] + " ");
}
}
}
0 12 23 45 90
一种值得学习的思路(双指针)
找到索引index后,复制在新的arr2里
for (int i = 0 ,j = 0 ; i < arr2.length ; i++){
if(i != index){
arr2[i] = arr[i];
j++;
}else{
arr2[i] = add;
}
}