Day06

一,数组的遍历
–1,语法
for(int i=0;i<a.length;i++){
syso(a[i]);
}
–2,代码
package cn.tedu.array;
import java.util.Random;
// 测试 数组的遍历
public class Test1 {
public static void main(String[] args) {
// method();//遍历数组
// method2();//输出每个月的天数
// method3();//遍历数组,存入1到10
// method4();//创建数组,存入随机数
method5();//统计数组里的奇数个数
}
//统计数组里的奇数个数
private static void method5() {
//1,创建数组
int a[] = {98, 2, 81, 49, 96, 8, 83, 68, 95, 34};

			int count = 0 ;//定义变量,记录个数
			//2,遍历数组,获取每个数据a[i]
			for (int i = 0; i < a.length; i++) {
				//过滤奇数
				if( a[i] % 2 == 1 ){
					//求个数++ 
					count++;
				}
			}
			//3,打印奇数的个数
			System.out.println("数组里的奇数的个数是: "+count);
		}
		//创建数组,存入随机数
		private static void method4() {
			//1,创建数组
			int[] a = new int[10];//数组创建完的数据都是默认值
			//2,遍历数组,修改 默认值
			for (int i = 0; i < a.length; i++) {
				//把给每个默认值改成随机数
				a[i] = new Random().nextInt(100)  + 1 ;
			}
			//3,遍历数组,并打印
			for (int i = 0; i < a.length; i++) {
				//根据下标i获取数据
				System.out.print( a[i]+", " ); 
			}
			
		}
		//遍历数组,存入1到10
		private static void method3() {
			//1,创建数组
			int[] a = new int[10];
			System.out.println(a);//a是引用类型的变量,存的是地址值[I@1175e2db
			//2,遍历数组,存入1-10
			for( int i = 0 ; i < a.length ; i++ ) {
				//根据下标  改 默认值
				a[i] = i+1 ;
			}
			//3,打印查看
			for( int i = 0 ; i < a.length ; i++ ) {
				//根据下标 打印
				System.out.println(a[i]);
			}
		}
		//输出每个月的天数
		private static void method2() {
			//1,定义数组
			int[] a = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
			//2,遍历数组--根据下标
			for(int i = 0;i <= a.length-1 ; i++ ) {
				//a[i],,,是根据下标i获取a数组里的数据
				//i+1里i是0~11 ,,,+1就是1~12了
				System.out.println(i+1+"月有"+a[i]+"天");
			}
		}
		//遍历数组
		private static void method() {
			//1,创建数组
			int[] a = new int[] {1,2,3,4,5,6,7,8,8};
			//2,打印数组里的数据---不好,数据很多的话,不可能一行一行的输出...
			System.out.println( a[0] );
			System.out.println( a[1] );
			System.out.println( a[2] );
			System.out.println( a[3] );
			//下标的取值范围 可以是 <= a.length-1
			System.out.println( a[4] );
			System.out.println( a.length );
		//抛出错误ArrayIndexOutOfBoundsException访问了不存在的下标5
		//				System.out.println( a[5] );
			
			//TODO 3,使用for循环结构--根据下标遍历数组里的数据
			/*
			 * int i = 0 是指 从下标为0的位置开始
			 * i <= a.length-1 是指 下标的取值范围是 <= 长度-1
			 * i++ 是指 下标的变化规律,按照+1递增
			 * i 表示下标
			 */
			for(int i = 0 ;i <= a.length-1 ; i++) {
				//根据下标i 打印 a数组里的数据
				System.out.println( a[i] );
			}
		}
		
	}

二,数组工具类Arrays
–1,概述
为数组的常见操作提供了各种方便的方法
–2,常用方法
–toString()–把数组里的数据变成串展示
–sort()–把无序的数组排序
–copyOf()–复制数组
–3,测试
package cn.tedu.array;
import java.util.Arrays;
//测试 工具类Arrays
//总结:
//1, toString(?)–要展示 数组里的 数据的
//2, sort(?)–给数组 排序的
//3, copyOf(1,2)–1是原元数组名–2是新数组的长度
//数组的特点:::数组的长度一旦定义就不能改变了
public class Test2_Arrays {
public static void main(String[] args) {
int a[] = {98, 2, 81, 49, 96, 8, 83, 68, 95, 34};
// method(a);//toString()打印数组里的数据
// method2(a);//sort()给数组排序
method3(a);//copyOf()复制数组
}
//copyOf()复制数组
private static void method3(int[] a) {
//TODO 为什么copyOf()有返回值/sort()没有返回值?–数组一旦创建长度不能改
//第一个参数是要一个原元数组,第二个参数是新的长度
int b[] = Arrays.copyOf(a, 12);
System.out.println( b );//[I@1175e2db

	//[98, 2, 81, 49, 96, 8, 83, 68, 95, 34, 0, 0] --扩容
			System.out.println( Arrays.toString(b) );
			
			int c[] = Arrays.copyOf(a, 8);
	//[98, 2, 81, 49, 96, 8, 83, 68]--缩容
			System.out.println( Arrays.toString(c) );
		}
		//sort给数组排序
		private static void method2(int[] x) {
			//sort(n)--给指定的数组n排序,默认是从小到大
			Arrays.sort(x);
			//[2, 8, 34, 49, 68, 81, 83, 95, 96, 98]
			System.out.println(  Arrays.toString(x) );
		}
		//创建method()--匹配 实参 a 的类型
		public static void method(int[] x) {
			//TODO 自己 打印数组里的数据
	//		for (int i = 0; i < x.length; i++) {
	//			System.out.println( x[i] );
	//		}
			//TODO 使用工具类 打印数组里的数据
			//toString(n)--可以把指定数组n里的数据变成字符串展示
			String datas = Arrays.toString(x);
			//[98, 2, 81, 49, 96, 8, 83, 68, 95, 34]
			System.out.println(datas);
			
			System.out.println( Arrays.toString(x) ) ;
		}
	}

三,类和对象的创建和使用
–1,概述
类是抽象的,可以理解成一个图纸一个模板
对象是具体的,可以用了描述类里的信息
类里有 成员变量和方法
–2,创建
类通过关键字class创建
对象通过过关键字new创建–数组是对象
–3,测试汽车类
package cn.tedu.oop;
//测试 创建对象/创建类
//一个.Java文件中,可以有多个类,但是只能有一个被public修饰.
//而且这个public的类名就是.Java文件名
public class Test3_New {
public static void main(String[] args) {
//2,创建对象测试
new Car().fly();//匿名对象–一次就干一个活儿

			Car c = new Car();//有名字的对象,叫c
			//3, .调用模板里的方法
			c.go();
			c.back();
			c.fly();
			//4, .调用模板里的属性
			c.color = "black";//设置值
			c.pinpai="50"; 
			c.price=2;
			System.out.println( c.color );//获取值null->black
			System.out.println( c.pinpai );//null->50
			System.out.println( c.price );//0.0->2
		}
	}
	//1,创建汽车类,,,来描述生活中的汽车事物
	class Car{
		//属性->成员变量::在类里方法外::引用类型的默认值都是null
		String color;//颜色
		String pinpai;//品牌
		double price;//价格
		//行为->方法::修饰符 返回值 方法名(参数列表){方法体}
		public void go() {//前进
			System.out.println("汽车在前进");
		}
		public void back(){//后退
			System.out.println("汽车在后退");
		}
		public void fly(){//飞
			System.out.println("汽车在飞");
		}
	}

--4,练习手机类
	package cn.tedu.oop;

	import java.awt.Point;

	//测试 创建对象/创建类
	public class Test4_New {
		public static void main(String[] args) {
			//TODO 3,创建对象测试
			new Phone().call();//匿名对象,只执行一个任务
			Phone p = new Phone();
			//调用模板里的方法
			p.call();
			p.play();
			//调用模板里的属性
			//设置值
			p.color = "yellow" ;
			p.pinpai = "8848" ;
			p.price = 8848 ;
			p.size = 2.5;
			//获取值
			System.out.println( p.color );//null->yellow
			System.out.println( p.pinpai );//null->8848
			System.out.println( p.price );//0.0->8848
			System.out.println( p.size );//0.0->2.5
		}
	}
	//1,创建手机类,描述手机事物
	class Phone{
		//2,提供属性 + 行为
		String color;
		String pinpai;
		double price;
		double size;
		public void call() {
			System.out.println("打电话");
		}
		public void play() {
			System.out.println("打游戏");
		}
	}

扩展:
–0,安装IDEA开发工具,做HelloWorld
–0,java的内存管理
–1,数组的练习
–随机打印数组里的数据
–获取数组里的最大值最小值
–统计数组里的数据总和和平均值
–统计数组里偶数的和
–模拟数组复制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值