数组概述:一次性声明大量的用于存储数据的变量,要存储的通常是同类型数据
数组定义:是一种用于存储多个相同数据的存储模型,
int [] arr=new int[2]; new代表为数组申请内存空间。
public static void main(String[] args) {
int [] arr={11,22,33,44,55};
int max=arr[0];
for(int x=1;x<arr.length;x++){
if(arr[x]>max){
max=arr[x];
}
}
System.out.println("最大值为:"+max);
}
arrays类:该类包含用于操作数组的各种方法:
* public static String toString(int []a):返回指定的数组的内容的字符串的表现形式
* public static void sort(int []a):按照数字顺序排列指定的数组
public class Arrays01 {
public static void main(String[] args) {
int[] arr={12,23,58,49};
System.out.println("排序前"+Arrays.toString(arr));
Arrays.sort(arr);
System.out.println("排序后"+Arrays.toString(arr));
}
}
-----------------------------
逢七过实例:
public class test01 {
public static void main(String[] args) {
for(int a=1;a<101;a++){
if(a%10==7 || a/10%10==7 || a%7==0){
System.out.println(a);
}
}
}
}
----------------------------不死神兔
public class test01 {
public static void main(String[] args) {
int[] arr = new int[10];
arr[0] = 1;
arr[1] = 1;
arr[2] = 2;
for (int a = 2; a < arr.length; a++) {
arr[a] = arr[a - 1] + arr[a - 2];
}
System.out.println(arr[4]);
}
}
------------------------
什么是方法:
是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集。
注意:方法必须创建后才可以使用,该过程称为定义
方法创建后并不是直接运行的,需要手动使用后才执行,该过程称为 方法调用。
有参方法记得用返回值接收。
定义方法时:要做到两个明确,
明确返回值类型,明确参数
static方法可以直接调用,非static修饰方法必须创建对象调用
方法的重载:
同一个类中定义多个方法之间的关系,
多个方法在同一个类中
相同的方法名
参数类型不同或者数量不同
注意:返回类型不能作为重载的依据。
对于引用类型的参数,像数组,形势参数改变,影响实际参数的值*/
--------------------------
public class test01 {
public static void main(String[] args) {
boolean aa=aa(100);
System.out.println(aa);
}
public static boolean aa(int aa){
if(aa%2==0){
return true;
}
else{
return false;
}
}
}
--------------------
方法重载练习:
public class test01 {
public static void main(String[] args) {
System.out.println(aa(10,20));
System.out.println(aa((byte) 10,(byte) 20));
}
public static boolean aa(int a,int b){
System.out.println("int");
return a==b;
}
public static boolean aa(byte a,byte b){
System.out.println("byte");
return a==b;
}
}
------------------------------------
不死神兔:
public class test01 {
public static void main(String[] args) {
int[] arr = new int[10];
arr[0] = 1;
arr[1] = 1;
arr[2] = 2;
for (int a = 2; a < arr.length; a++) {
arr[a] = arr[a - 1] + arr[a - 2];
}
System.out.println(arr[4]);
}
}