-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
803bfcb
commit c8c8a7c
Showing
41 changed files
with
941 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
方法重载: | ||
在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。 | ||
和返回值类型无关。 | ||
*/ | ||
class MethodDemo { | ||
public static void main(String[] args) { | ||
//需求:请写一个功能,实现两个数据的求和 | ||
System.out.println(sum(10,20)); | ||
|
||
//需求:请写一个功能,实现三个数据的求和 | ||
//System.out.println(sum2(10,20,30)); | ||
System.out.println(sum(10,20,30)); | ||
|
||
System.out.println(sum(1.5,2.5)); | ||
} | ||
|
||
public static int sum(int a,int b) { | ||
return a + b; | ||
} | ||
|
||
/* | ||
public static int sum2(int a,int b,int c) { | ||
return a + b + c; | ||
//方法的嵌套调用 | ||
//return sum(sum(a,b),c); | ||
} | ||
*/ | ||
|
||
//由于方法名要表达的其实是该方法的作用 | ||
//所以,sum2这个名字不好,还是要用sum 比较好 | ||
public static int sum(int a,int b,int c) { | ||
return a + b + c; | ||
} | ||
|
||
public static double sum(double a,double b) { | ||
return a + b; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
比较两个数据是否相等。参数类型分别为两个byte类型,两个short类型, | ||
两个int类型,两个long类型,并在main方法中进行测试 | ||
*/ | ||
class MethodTest { | ||
public static void main(String[] args) { | ||
//测试 | ||
byte a = 10; | ||
byte b = 10; | ||
System.out.println(compare(a,b)); | ||
|
||
System.out.println(compare(10,10)); | ||
} | ||
|
||
//byte | ||
/* | ||
public static boolean compare(byte a,byte b) { | ||
System.out.println("byte"); | ||
return a == b; | ||
} | ||
//short | ||
public static boolean compare(short a,short b) { | ||
System.out.println("short"); | ||
return a == b; | ||
} | ||
//int | ||
public static boolean compare(int a,int b) { | ||
System.out.println("int"); | ||
return a == b; | ||
} | ||
*/ | ||
|
||
//long | ||
public static boolean compare(long a,long b) { | ||
System.out.println("long"); | ||
return a == b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
数组:存储同一种数据类型的多个元素的集合。(也可以称为容器) | ||
数组的定义格式: | ||
格式1:数据类型[] 数组名; | ||
int[] arr; | ||
格式2:数据类型 数组名[]; | ||
int arr[]; | ||
推荐方式1。 | ||
现在的数组没有元素,使用是没有意义的。 | ||
接下来,我们要对数组进行初始化。 | ||
那么,我们如何对数组进行初始化呢? | ||
动态初始化:初始化时只指定数组长度,由系统为数组分配初始值。 | ||
静态初始化:初始化时指定每个数组元素的初始值,由系统决定数组长度。 | ||
动态初始化: | ||
数据类型[] 数组名 = new 数据类型[数组长度]; | ||
*/ | ||
class ArrayDemo { | ||
public static void main(String[] args) { | ||
//按照动态初始化数组的格式,我们来定义一个存储3个int类型元素的数组 | ||
int[] arr = new int[3]; | ||
|
||
/* | ||
左边: | ||
int:说明数组中的元素的数据类型。 | ||
[]:说明这是一个数组 | ||
arr:数组的名称 | ||
右边: | ||
new:为实体(对象)开辟内存空间 | ||
Scanner sc = new Scanner(System.in); | ||
int:说明数组中的元素的数据类型。 | ||
[]:说明这是一个数组 | ||
3:说明的是数组中的元素个数 | ||
*/ | ||
|
||
//我们如何获取里面的值呢? | ||
//数组名称 | ||
System.out.println(arr); //[I@778b3fee 地址值 | ||
//如何获取元素值呢? | ||
//数组为每个元素分配了一个编号,这个编号的专业叫法:索引。 | ||
//而且是从0开始编号的。也就是说数组的最大编号是长度-1。 | ||
//有了编号以后,我们就可以通过数组名和编号的配合取得数组元素 | ||
//格式:数组名[编号]; | ||
System.out.println(arr[0]); //0 | ||
System.out.println(arr[1]); //0 | ||
System.out.println(arr[2]); //0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
定义一个数组,输出数组名及元素。然后给数组中的元素赋值,再次输出数组名及元素。 | ||
*/ | ||
class ArrayDemo2 { | ||
public static void main(String[] args) { | ||
//定义数组 | ||
int[] arr = new int[3]; | ||
|
||
//输出数组名称及元素 | ||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
|
||
//给数组中的元素赋值 | ||
arr[0] = 10; | ||
arr[1] = 20; | ||
arr[2] = 30; | ||
|
||
//再次输出数组名称及元素 | ||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
定义两个数组,分别输出数组名及元素。然后分别给数组中的元素赋值,分别再次输出数组名及元素。 | ||
*/ | ||
class ArrayDemo3 { | ||
public static void main(String[] args) { | ||
//定义一个数组 | ||
int[] arr = new int[3]; | ||
|
||
//输出数组名称及元素 | ||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
|
||
//赋值并输出 | ||
arr[0] = 10; | ||
arr[1] = 20; | ||
arr[2] = 30; | ||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
|
||
//再定义一个新的数组 | ||
int[] arr2 = new int[2]; | ||
System.out.println(arr2); | ||
System.out.println(arr2[0]); | ||
System.out.println(arr2[1]); | ||
|
||
//赋值并输出 | ||
arr2[0] = 100; | ||
arr2[1] = 200; | ||
System.out.println(arr2); | ||
System.out.println(arr2[0]); | ||
System.out.println(arr2[1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
定义两个数组,先定义一个数组,赋值,输出。 | ||
然后定义第二个数组的时候把第一个数组的地址赋值给第二个数组。 | ||
然后给第二个数组赋值,再次输出两个数组的名及元素。 | ||
*/ | ||
class ArrayDemo4 { | ||
public static void main(String[] args) { | ||
//定义一个数组 | ||
int[] arr = new int[3]; | ||
|
||
//赋值,输出。 | ||
arr[0] = 11; | ||
arr[1] = 22; | ||
arr[2] = 33; | ||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
|
||
//定义第二个数组 | ||
int[] arr2 = arr; | ||
|
||
arr2[1] = 100; | ||
|
||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); //??? | ||
System.out.println(arr[2]); | ||
|
||
System.out.println(arr2); | ||
System.out.println(arr2[0]); | ||
System.out.println(arr2[1]); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lvdeng
via email
|
||
System.out.println(arr2[2]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
静态初始化格式: | ||
数据类型[] 数组名 = new 数据类型[]{元素1,元素2,…}; | ||
简化版: | ||
数据类型[] 数组名 = {元素1,元素2,…}; | ||
*/ | ||
class ArrayDemo5 { | ||
public static void main(String[] args) { | ||
//定义一个数组 | ||
//int[] arr = new int[]{1,2,3}; | ||
|
||
//简化后 | ||
int[] arr = {1,2,3}; | ||
|
||
System.out.println(arr); | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
数组操作常见的两个小问题: | ||
A:数组索引越界 | ||
ArrayIndexOutOfBoundsException | ||
因为我们访问了不存在的索引。 | ||
B:空指针异常 | ||
NullPointerException | ||
因为数组已经不再指向堆内存,所以就不能再去访问堆内存的元素了。 | ||
*/ | ||
class ArrayDemo6 { | ||
public static void main(String[] args) { | ||
//定义数组 | ||
int[] arr = {1,2,3}; | ||
|
||
//System.out.println(arr[3]); | ||
|
||
arr = null; //把arr指向堆内存给去掉了,arr没有指向了。 | ||
System.out.println(arr[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
数组遍历: | ||
依次输出数组中的每一个元素 | ||
分析: | ||
A:定义一个数组,并赋值。 | ||
B:输出数组中的元素 | ||
*/ | ||
class ArrayTest { | ||
public static void main(String[] args) { | ||
//定义一个数组,并赋值 | ||
int[] arr = {11,22,33,44,55}; | ||
|
||
//方式1 | ||
System.out.println(arr[0]); | ||
System.out.println(arr[1]); | ||
System.out.println(arr[2]); | ||
System.out.println(arr[3]); | ||
System.out.println(arr[4]); | ||
System.out.println("---------------"); | ||
|
||
//方式2 | ||
//通过观察,我们可以通过for循环实现取出0~4就OK了 | ||
for(int x=0; x<5; x++) { | ||
System.out.println(arr[x]); | ||
} | ||
System.out.println("---------------"); | ||
|
||
//方式3 | ||
//如果元素多,数起来太麻烦了。 | ||
//所以呢,数组为了方便我们的操作,就自己提供了一个属性:长度属性 length | ||
//用来获取数组的长度。 | ||
//格式是:数组名.length; | ||
//System.out.println(arr.length); | ||
|
||
for(int x=0; x<arr.length; x++) { | ||
System.out.println(arr[x]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
数组获取最值(获取数组中的最大值最小值) | ||
自学:最小值。 | ||
*/ | ||
class ArrayTest2 { | ||
public static void main(String[] args) { | ||
//定义数组 | ||
int[] arr = {12,86,79,43,50}; | ||
|
||
//如何找呢? | ||
/* | ||
int max = arr[0]; | ||
for(int x=1; x<arr.length; x++) { | ||
if(arr[x] > max) { | ||
max = arr[x]; | ||
} | ||
} | ||
System.out.println(max); | ||
*/ | ||
|
||
//为了将来调用方便,我们写一个功能实现获取最值 | ||
int max = getMax(arr); | ||
System.out.println("最大值是:"+max); | ||
} | ||
|
||
/* | ||
返回值类型:int | ||
参数列表:int[] arr | ||
*/ | ||
public static int getMax(int[] arr) { | ||
//定义参照物 | ||
int max = arr[0]; | ||
|
||
//循环遍历从1开始元素 | ||
for(int x=1; x<arr.length; x++) { | ||
//比较,把大的留下来 | ||
if(arr[x] > max) { | ||
max = arr[x]; | ||
} | ||
} | ||
|
||
//返回最大值 | ||
return max; | ||
} | ||
} |
Oops, something went wrong.
//arr2[1]=100;