韩顺平JAVA-D18

0155 数组的必要性

0156 数组快速入门

0157 - 0159 数组的使用

0160 数组的注意事项

0161 - 0162 数组练习1

//创建一个char类型的数组,放置A-Z,使用for 循环访问所有元素并打印
public class ArrayExercise01{
	public static void main(String args[]){
		char a[] = new char[26];
		for(int i = 0; i < 26; i++){
			a[i] = (char)('A'+ i);
			//a[i] = a[i-1] + 1;
			System.out.print(a[i] );
		}
	}
}

//求Int数组的最大值,并输出下标
public class ArrayExercise02{
	public static void main(String args[]){
		int a[] = {4,-1,9,10,23};
		int max = a[0];
		int j = 0;
		for (int i = 1; i< a.length; i++){
			if(a[i] >= max){
				max = a[i];
				j = i;
			}
		}
		System.out.println("数组最大值为:" + max + "下标为:" + j);
	}
}

0163 数组赋值机制

0164 数组赋值机制2

0165 数组拷贝

给arr2开辟新地址,按数组每个元素拷贝,不会拷贝地址,修改arr2不会影响arr1的值。

0166 数组翻转1

//逆序赋值的方法将{11,22,33,44,55,66}翻转
public class ArrayReverse{
	public static void main(String args[]){
		int a[] = {11,22,33,44,55,66};
		int b[] = new int[a.length];
		for (int i = 0; i < 2; i++){
			b[i] = a[i];
			a[i] = a[5- i];
			a[5- i] = b[i];
		}
		for (int i = 0; i < a.length; i++){
			System.out.print(a[i] + " ");
		}
	}
}

0167 数组翻转02

//逆序赋值的方法将{11,22,33,44,55,66}翻转
public class ArrayReverse{
	public static void main(String args[]){
		int a[] = {11,22,33,44,55,66};
		int b[] = new int[a.length];
		/*for (int i = 0; i < 2; i++){
			b[i] = a[i];
			a[i] = a[a.length-1- i];//a[a.length-i]出现报错,数组里面不能用距离函数吗?
			a[5- i] = b[i];
			
		}
		for (int i = 0; i < a.length; i++){
			System.out.print(a[i] + " ");
		}*/
		for(int i = 0; i < a.length; i++){
			b[i] = a [a.length - 1 - i];
			System.out.print(b[i] + "\t");
		}
	}
}

0168数组扩容

import java.util.Scanner;
public class ArrayReduce{
	public static void main(String args[]){
		Scanner myScanner = new Scanner(System.in);
		int a[] = {1, 2, 3, 4, 5};
		do{
			for(int i = 0; i < a.length; i++){
				System.out.print(a[i] + "\t");
			}
			System.out.println("是否要删减?y/n");
			char k = myScanner.next().charAt(0);
			if(k == 'n' ){
				break;
			}else if(a.length == 1){
				System.out.println("不能再删减了");
				break;
			}
			int b[] = new int[a.length - 1];
			for(int j = 0; j < b.length; j++){
				b[j] = a[j];
			}
			a = b;
		}while (true);		
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值