数组——基础操作练习

数组的遍历

class Demo01 {
	public static void main(String[] args) {
		int[] arr = {67, 45, 34, 56, 23, -88, 666};
		//通过循环,获取数组中所有的索引
		for (int i = 0; i < arr.length ; i++ ) {
			//通过索引访问对应位置的元素
			System.out.println(arr[i]);
		}
	}
}

数组获取最大值


class Demo1 {
	public static void main(String[] args) {
		int[] arr = {-67, -45, -34, -56, -23, -88, -666};
		//声明一个变量,用于存储当前的最大值,相当于擂台
		int max = arr[0];//擂台上只能存在参赛选手
		//循环遍历数组中的所有元素,拿每个元素和max比较,如果比max大,就将当前元素赋值给max
		for (int i = 1; i < arr.length ; i++ ) {
			if (arr[i] > max) {
				max = arr[i];
			}
		}
		System.out.println(max);
	}
}

数组的反转

class Demo1 {
	public static void main(String[] args) {
		int[] arr = {67, 45, 34, 56, 23, -88, 666};
		//定义循环,定义两个指针,分别表示开始和结束的索引,这两个索引总是对称的
		for (int i = 0, j = arr.length - 1; i < j ; i++, j-- ) {
			//i和j两个位置上的元素交换
			int temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
		//遍历数组
		for (int i = 0; i < arr.length ; i++ ) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值