一,数组练习
–1,存入hello
package cn.tedu.basic;
//测试 数组
public class Test1 {
public static void main(String[] args) {
//1,创建数组,存入hello
//静态初始化
char[] a = new char[] {‘h’,‘e’,‘l’,‘l’,‘o’};
char[] b = {‘h’,‘e’,‘l’,‘l’,‘o’};
System.out.println(a);
System.out.println(b);
//动态初始化
char[] c = new char[5];
c[0] = ‘h’ ;//修改
System.out.println(c[0]);//查看
//TODO 修改其他位置的默认值\u0000
c[1] = ‘e’ ;//修改
c[2] = ‘l’ ;//修改
c[3] = ‘l’ ;//修改
c[4] = ‘o’ ;//修改
//TODO 数组下标越界异常:因为访问了不存在的下标
//java.lang.ArrayIndexOutOfBoundsException:
// c[5] = ‘x’ ;//不存在的下标5
System.out.println©;
}
}
--2,遍历数组
package cn.tedu.basic;
import java.util.Random;
//测试 遍历数组
public class Test2 {
public static void main(String[] args) {
// method();//遍历数组的入门
// method2();//输出每个月的天数
// method3();//存入1到10
// method4();//存入10个随机数
method5();//统计数组里 偶数的和
}
//统计数组里 偶数的和
public static void method5() {
//1,创建数组
int[] a = {37,65,28,22,57,70,40,96,57,50};
//2,遍历数组
int sum = 0;//定义变量,记录和
for (int i = 0; i < a.length; i++) {
//过滤出来偶数
if(a[i] % 2 == 0 ) {
//修改和=原来的值+新偶数
sum = sum + a[i] ;
}
}
System.out.println(sum);//打印和
}
//存入10个随机数
public static void method4() {
//1,创建数组
int a[] = new int[10];
//2,遍历数组
for (int i = 0; i < a.length; i++) {
//把每个位置的元素,从默认值修改成 随机数
a[i] = new Random().nextInt(100);
}
//3,打印数组里的数据
System.out.println(a);//[I@6d06d69c地址值
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
}
//存入1到10
public static void method3() {
//1,创建数组
int a[] = new int[10];
//2,遍历数组
for(int i = 0 ; i <= a.length-1 ;i++) {
a[i] = i+1 ; //改
System.out.println( a[i] );//查看
}
}
//输出每个月的天数
public static void method2() {
//1,创建数组
int[] a = {31,29,31,30,31,30,31,31,30,31,30,31};
//2,根据下标遍历数组
for(int i = 0 ;i < a.length ;i++ ) {
//i+1是为了取到1~12月,i是0~11,加1就是1~12
//a[i]是根据下标获取a数组里的数据
System.out.println(i+1+"月有"+a[i]+"天");
}
}
//遍历数组的入门
public static void method() {
//1,创建数组
//a是引用类型的变量,存的是地址值。
int[] a = new int[5];//a数组现在存的是默认值0
System.out.println(a);//[I@15db9742,地址值
//2,遍历数组--根据下标遍历
// System.out.println(a[0]);
// System.out.println(a[1]);
// System.out.println(a[2]);
// System.out.println(a[3]);
// System.out.println(a[4]);
/*int i = 0 是指从下标为0的位置开始,i表示下标
* i <= a.length-1 是指下标可以取到的最大值是长度-1
* i++ 是指下标的变化规律 ,自增
*/
for(int i = 0 ; i <= a.length-1 ; i++ ) {
a[i] = 1;//修改
System.out.println(a[i]);//根据下标,输出a数组里的数据
}
}
}
二,数组工具类Arrays
–1,概述
专门为数组提供了很多方法。
–2,常用方法
toString()–把数组里的数据展示成字符串
sort()–把数组里的数据进行排序
copyOf()–复制数组里的数据
–3,测试
package cn.tedu.basic;
import java.util.Arrays;
//测试 数组的工具类
public class Test3 {
public static void main(String[] args) {
// method();//toString()
// method2();//sort()
method3();//copyOf()
}
//copyOf()
public static void method3() {
//1,创建数组
int[] a = {5,3,1,2,8,6};
//复制-copyOf(1,2)-1是原数组名-2是新长度
int[] b = Arrays.copyOf(a,10);
//展示b数组里的数据-[5, 3, 1, 2, 8, 6, 0, 0, 0, 0]--扩容(新长度>旧的)
System.out.println( Arrays.toString(b));
int[] c = Arrays.copyOf(a,3);
//展示c数组里的数据-[5, 3, 1]--缩容(新长度<旧的)
System.out.println( Arrays.toString(c));
}
//sort()
public static void method2() {
//1,创建数组
int[] a = {5,3,1,2,8,6};
//排序 -- 默认升序,从小到大
Arrays.sort(a) ;
//打印 -- [1, 2, 3, 5, 6, 8]
System.out.println( Arrays.toString(a) );
System.out.println( a[0] );//获取数组里的最小值
System.out.println( a[a.length-1] );//获取数组里的最大值
}
//toString()
public static void method() {
//1,创建数组
String[] a = {"迪丽热巴","杨幂","皮皮霞"};
//2,查看数组里的数据
System.out.println(a);//[Ljava.lang.String;@15db9742
// for (int i = 0; i < a.length; i++) {
// System.out.println(a[i]);
// }
//TODO 用工具类优化 toString(a)--展示a数组中的数据
String res = Arrays.toString(a);
System.out.println(res);//[迪丽热巴, 杨幂, 皮皮霞]
//TODO 继续优化,省一个变量
System.out.println( Arrays.toString(a) );
}
}
三,类和对象的创建
–1,概述
类是一类事物的抽象,通过class关键字定义
对象是具体的,通过new关键字定义
–2,创建Phone类
package cn.tedu.oop;
//测试 oop
//一个.java文件中,可以存在多个class,
//要求:只能有一个被public修饰 ,.java的文件名 = public的类名
public class Test4 {
public static void main(String[] args) {
//2.通过new创建对象
new Phone().call();//匿名对象 + 一次只执行一个任务
//3.p是引用类型,存地址值
Phone p = new Phone();//有名字的对象
System.out.println§;//cn.tedu.oop.Phone@15db9742
Phone p2 = new Phone();//每次new都是产生新的对象
System.out.println(p2);//cn.tedu.oop.Phone@6d06d69c
//4.调用模板里的方法
p.game();
p.call();
p.message();
//5.调用模板里的属性
System.out.println( p.size );//0.0
System.out.println( p.pinpai );//null
System.out.println( p.color );//null
//设置属性的值
p.size = 5.7 ;
p.pinpai = “VIVO” ;
p.color = “blue” ;
//获取属性的值
System.out.println( p.size );//5.7
System.out.println( p.pinpai );//VIVO
System.out.println( p.color );//blue
//TODO 练习,给p2对象设置值/获取值
p2.game();
p2.call();
p2.message();
p2.size = 7.8 ;
p2.pinpai = "HUAWEI" ;
p2.color = "green" ;
System.out.println( p2.size );
System.out.println( p2.pinpai );
System.out.println( p2.color );
}
}
//1.创建手机类--用来描述手机事物
class Phone{
//特征--属性/成员变量(类里方法外)
double size ;//大小
String pinpai ;//品牌
String color ;//颜色
//功能--方法/成员方法(类里方法外)
//方法的修饰符 方法的返回值 方法名([参数列表]){方法体}
public void game() {
System.out.println("王者...");
}
public void call() {
System.out.println("打电话...");
}
public void message() {
System.out.println("发短信...");
}
}
扩展:
–1.自己实现 获取数组里的最小/大 值
–2.统计取数组里所有数字的总和、平均值