数组的使用

数组的常用方法

// 获取数组中最大的数
	public static int a18(int[] a) {
		int max = a[0];
		for (int i = 0; i < a.length; i++) {
			if (a[i] > max) {
				max = a[i];
			}
		}
		return max;
	}

	// 获取最大值下标
	public static int a19(int[] a) {
		int maxindex = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] > a[maxindex]) {
				maxindex = i;
			}
		}
		return maxindex;
	}

//从控制台输入10个数,把这10个存到整型数组中
	public static void a20() {
		System.out.println("请输入10个整数:");
		Scanner scan = new Scanner(System.in);
		int[] arr = new int[10];
		for (int i = 0; i < arr.length; i++) {
			System.out.print("请输入第" + (i + 1) + "个数:");
			arr[i] = scan.nextInt();
		}
		System.out.println("数组中的值:");
		for (int i : arr) {
			System.out.print(i + " ");
		}

	}

	// 将任意一个整型数组进行逆序存储
	public static void a21() {
		int[] a = new int[] { 1, 2, 3, 4, 5 };
		int[] b = new int[5];
		// 打印原数组a中的元素
		System.out.print("原数组:");
		for (int i : a) {
			System.out.print(i + " ");
		}
		System.out.println();
		// 将a数组中的元素逆序排序并赋值给数组b
		// i=0 是从数组下标为0时开始遍历
		// j=a.length-1 是从数组的最后的下标开始遍历
		for (int i = 0, j = a.length - 1; i < a.length; i++, j--) {
			b[j] = a[i];
		}
		// 将逆序的b数组中的元素重新赋值给a数组
		System.out.print("逆序后的数组:");
		for (int i = 0; i < a.length; i++) {
			a[i] = b[i];
			System.out.print(a[i] + " ");
		}
	}

//冒泡排序大到小
	public static void a22() {
		int[] a = new int[] { 2, 0, 1 };
		for (int i = 0; i < a.length - 1; i++) {
		for(int j=0;j<a.length-i-1;j++){
		
			if (a[j] < a[j + 1]) {
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}}
		}
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]);
		}
		System.out.println();
	}

	// 位置交换倒序
	public static void a23() {
		int[] arr = new int[] { 5, 3, 7, 1, 6 };
		System.out.println("原数组:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]);
		}
		// 交换
		for (int i = 0; i < arr.length / 2; i++) {
			int temp = arr[i];
			// arr.length - 1 - i
			// arr.length - 1获取下标
			// arr.length - 1 - i获取前一个下标
			arr[i] = arr[arr.length - 1 - i];
			arr[arr.length - 1 - i] = temp;
		}
		System.out.println("\n新数组:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]);
		}
		System.out.println();

	}

	// 无序数组插入数据
	public static void a24() {
		int a[] = new int[] { 1, 3, 5, 8 };
		int b = 2;
		int c[] = new int[a.length + 1];
		for (int i = 0; i < a.length; i++) {
			c[i] = a[i];
		}
		// 數組複製到另一數組原本順序不變
		// 將b賦值給c數組的最大下標
		c[c.length - 1] = b;

		for (int i = 0; i < c.length; i++) {
			System.out.print(c[i]);
		}
	}

	// 有序数组插入
	public static void a25() {
		int a[] = new int[] { 1, 3, 5, 8 };
		// c=true 从小到大 c=false 从大到小
		boolean c = a[0] < a[a.length - 1];
		int index = -1;
		for (int i = 0; i < a.length; i++) {
			if (a[i] > 6) {
				index = i;
				break;
			}
		}
		int[] b = new int[a.length + 1];
		for (int i = 0; i < index; i++) {
			b[i] = a[i];
		}
		b[index] = 6;
		for (int i = index + 1; i < b.length; i++) {
			b[i] = a[i - 1];
		}
		for (int i : b)
			System.out.print(i);
	}

	// 合并数组的方法
	// 参数 数组引用 1 2
	// 返回值 合并后的数组
	public static int[] a26(int[] a, int[] b) {
		if (a != null && b != null && a.length > 0 && b.length > 0) {
			int c[] = new int[a.length + b.length];
			for (int i = 0; i < a.length; i++) {
				c[i] = a[i];
			}
			for (int i = a.length; i < c.length; i++) {
				c[i] = b[i - a.length];
			}
			return c;
		} else {
			System.out.println("传入的数组有误");
		}
		return null;
	}

	// 已知两个数组{1,3,4,} {2,5,8} 合并成一个数组
	public static void a27() {
		int a[] = new int[] { 1, 3, 4 };
		int b[] = new int[] { 2, 5, 8 };
		int c[] = new int[a.length + b.length];
		for (int i = 0; i < a.length; i++) {
			c[i] = a[i];
		}
		// 因为前一个数组已经把数据添加到c数组中,所以循环时从数组a的长度开始
		// c[i] = b[i-3]
		// b数组 下标为 0-2 c数组中已占有位置的下标为0-2 所以 c中只有3-c数组长度-1 有空位
		// 为得到b数组中数据 需要 -3才可以得到
		for (int i = a.length; i < c.length; i++) {
			c[i] = b[i - 3];
		}
		for (int i = 0; i < c.length; i++) {
			System.out.print(c[i]);
		}
	}

	// 输入一行字符串,分别统计其中英文字母,空格,数字和其他字符 个数
	public static void a28() {
		System.out.println("请输入一串字符:");
		Scanner scan = new Scanner(System.in);
		String str = scan.nextLine();
		char[] chs = str.toCharArray();
		int numCount = 0;
		int charCount = 0;
		int spaceCount = 0;
		int otherCount = 0;
		for (int i = 0; i < chs.length; i++) {
			if (chs[i] == ' ') {
				spaceCount++;
			} else if (chs[i] >= '0' && chs[i] <= '9') {
				numCount++;
			}
			if (chs[i] >= 'a' && chs[i] <= 'z' || chs[i] >= 'A' && chs[i] <= 'Z') {
				charCount++;
			} else {
				otherCount++;
			}
		}
		System.out.println("字母个数:" + charCount + "\r\n 数字个数:" + numCount + "\r\n 其他字符个数:" + otherCount + "\r\n 空格个数:"
				+ spaceCount);
	}

	// 参数:数组引用,要插入的数据,插入的位置
	// 返回值:插入完以后的数组
	public static void a29() {
		int a[] = new int[] { 1, 2, 3, 5, 6, 7, 8, 9, 10 };
		int[] newA = a30(a, 4, 2);
		for (int i : newA) {
			System.out.print(i + " ");
		}

	}

	// 向数组中插入数据的方法 传入一个数组 和一个要插入的数
	public static int[] a30(int[] a, int num, int index) {
		// 传入时验证
		if (a != null && a.length > 0) {
			if (index > -1 && index < a.length) {
				int[] newA = new int[a.length + 1];
				newA[index] = num;
				for (int i = 0; i < index - 1; i++) {
					newA[i] = a[i];
				}
				for (int i = index + 1; i < newA.length; i++) {
					newA[i] = a[i - 1];
				}
				return newA;
			} else {
				System.out.println("不能向" + index + "位置插入数据");
			}
		} else {
			System.out.println("传入数组不能为空,并且长度不能为1");
		}
		return null;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值