星期四, 十一月 12日 2020, 8:30 晚上

  1k 字     5 分钟       

Eclipse的基本操作和设置

从接触数组,我们就开始学习Eclipse!

install and settings

brew install eclipse-jee

image-20201112210137314

image-20201112212245108

image-20201112212442246

image-20201112212822288

image-20201112213256190

改成这个样子,然后

image-20201112210220171

Xnip2020-11-12_21-01-56

新建一个项目

image-20201112213104765

image-20201112210539410

image-20201112210622818

image-20201112210705906

image-20201112213328104

image-20201112213411099

image-20201112213416364

image-20201112213443187

image-20201112213517162

image-20201112213531669

image-20201112214037435

输入main,然后按option+ '/'即可自动补全为main方法

image-20201112214141554

输入syso, opt + '/'就补全为println!

image-20201112214302205

image-20201112214321448

image-20201112214351699

image-20201112214413707

这个也行

image-20201112214734656

导入已有的工程

image-20201115142345203

在代码中显示个人信息

image-20201115143312550

image-20201115143831064

image-20201115144037776

image-20201115144046280

然后只要在coding的时候package 下面一行输入/**然后敲回车即可自动补全信息

image-20201115144213594

image-20201115144301818

常用快捷键

image-20201115144418297

数组的学习

Python中数组的意思就是列表!

image-20201115144623354

image-20201115145000396

  • 先添加class注释
package com.aiguigu.contact;
/**
 * 
 * @Description
 * @Author Herman Zhao
 * @EMail hermanzhaozzzz@gmail.com
 * @Version
 * @Date 2020年11月15日下午2:50:21
 */
public class ArrayTest {
	public static void main(String[] args) { // 这个输入main按下opt+顿号直接就能补全
		
	}
}

一维数组的使用

package com.aiguigu.contact;
/**
 * 
 * @Description
 * @Author Herman Zhao
 * @EMail hermanzhaozzzz@gmail.com
 * @Version
 * @Date 2020年11月15日下午2:50:21
 */
public class ArrayTest {
	public static void main(String[] args) {
		// 一维数组的声明和初始化
		int num; //声明
		num = 10; //初始化
		int id = 1001; //声明+初始化
		
		int[] ids;
		//静态初始化
		ids = new int[] {1001,1002,1003,1004};
		
		//动态初始化
		String[] names = new String[5];
		
		//错误的写法:
//		int[] arr1 = new int[];
//		int[5] arr2 = new int[5];
//		int[] arr3 = new int[3] {1,2,3};
//		数组一旦初始化完成,其长度就确定了
		
		
		
//		调用数组
//		java的数组索引是从0开始到length-1结束
		names[0] = "wangming";
		names[1] = "wanghe";
		names[2] = "zhangxueliang";
		names[3] = "sunjulong";
		names[4] = "wanghongzhi";
//		names[5] = "wanssssghongzhi";
		
		
		//获取数组长度
		System.out.println(names.length); //5
		System.out.println(names[3]);
		
		
		//遍历数组
		for(int i = 0; i < names.length; i++) {
			System.out.println(names[i]);
		}
		
		
		//数组元素的默认初始化值
		int[] arr = new int[4];
		for(int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		
		System.out.println("*************");


		short[] arr1 = new short[4];
		for(int i = 0; i < arr1.length; i++) {
			System.out.println(arr1[i]);
		}
		
		System.out.println("*************");
		
		
		float[] arr2 = new float[5];
		for (int i = 0; i < arr2.length; i++) {
			System.out.println(arr2[i]);
		}
		System.out.println("*************");
		
		char[] arr3 = new char[4];
		for (int i = 0; i < arr3.length; i++) {
			System.out.println(arr3[i]);
		}
		
		
		System.out.println("*************");
		if (arr3[0] == 0) {
			System.out.println("确实不是空的,而是0");
		}
		
		
		boolean[] arr4 = new boolean[] {true,false};
		System.out.println(arr4[0]);
		
		
		
		String[] arr5 = new String[5];
		System.out.println(arr5[0]);
		if (arr5[0] == null) {
			System.out.println("北京天气不错!");
		}
		
	}
}

数组的内存解析

内存分配是在JVM中去规范的

image-20201115155425390

下面看看怎么回事

image-20201115155519441

image-20201115160227276

image-20201115160554697

image-20201115160642029

练习,程序员招租

image-20201115161010363

多维数组的使用

image-20201116195818900

image-20201116195906939

image-20201116200128624

image-20201116201106918

image-20201116201407005

image-20201116201615536

image-20201116201644256

image-20201116201806845

image-20201116201812231

image-20201116203002926

package com.aiguigu.contact;

public class ArrayTest3 {
	public static void main(String[] args) {
		int[][] arr = new int[4][3];
		System.out.println(arr[0]);
		System.out.println(arr[0][0]);
	}
}
// out 
[I@3cd1a2f1
0

[开头的那个是一维数组@后面是二进制地址

image-20201116203253938

二维数组的内存解析

image-20201116203712236

image-20201116203814379

image-20201116203852330

image-20201116203940714

初始化值

image-20201116212011700

练习,二维数组

问题:

image-20201116213321347

解决方案:

image-20201116213253296

package com.huanan.learn;

public class ArrayExer1 {
	public static void main(String[] args) {
		int[][] arr = new int [][] <!--0-->;
		
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				sum += arr[i][j];
			}
		}
		System.out.println(sum);
	}
}

问题:

image-20201116213607569

package com.huanan.learn;

public class YanghuiTest {
	public static void main(String[] args) {
		int [][] yanghui = new int[10][];
		
		for (int i = 0; i < yanghui.length; i++) {
			yanghui[i] = new int[i + 1];
			yanghui[i][0] = yanghui[i][i] = 1;
		}
		
		for (int i = 0; i < yanghui.length; i++) {
			for (int j = 0; j < yanghui[i].length; j++) {
				System.out.print(yanghui[i][j]);
			}
			System.out.println();
		}
	}
}


// out
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001

加入判断和赋值

package com.huanan.learn;

public class YanghuiTest {
	public static void main(String[] args) {
		int [][] yanghui = new int[10][];
		
		for (int i = 0; i < yanghui.length; i++) {
			yanghui[i] = new int[i + 1];
			yanghui[i][0] = yanghui[i][i] = 1;
			
			
//			赋值
			if(i > 1) {
				for(int j = 1; j < yanghui[i].length -1; j++) {
					yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j]; 
				}
			}
		}

		
		
		
//		print
		for (int i = 0; i < yanghui.length; i++) {
			for (int j = 0; j < yanghui[i].length; j++) {
				System.out.print(yanghui[i][j]);
			}
			System.out.println();
		}
	}
}




// out
1
11
121
1331
14641
15101051
1615201561
172135352171
18285670562881
193684126126843691

关于数组的常见算法

image-20201116215111269

Arrays工具类的使用

  • boolean equals(int[] a, int[] b) 判断两个数组是否相等
  • String toString(int[] a) 输出数组信息
  • void fill(int[] a, int val) 将指定值填充到数组之中
  • void sort(int[] a) 对数组进行排序
  • int binarySearch(int[] a, int key) 返回所在位置index,小于零则在数组中不存在!

image-20201116221235207

image-20201116221718805

image-20201116221937158

数组使用中的常见异常

  1. 数组角标越界的异常
    • ArrayIndexOutOfBoundsExcetion
  2. 空指针异常
    • NullPointerException


Java   学习资源整理   尚硅谷java零基础教程      java

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!

 目录

↓↓↓↓赞赏!
Continue