Java作业之实验三数组

1. 写一个类,在其中定义一些方法,实现数组元素的遍历、排序、插入、删除、查找。

package shiyan3;

public class shiyan3_1_Array {
	public static int arr[] = { 4, 5, 6, 1, 7, 2, 4, 3 };

	// 遍历
	public static void traverse() {
		System.out.println("遍历:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + "  ");
		}
	}

	// 排序(从大到小)
	public static void sort() {
		int temp;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length; j++) {
				if (j < arr.length - 1) {// 保证最后一个时不会超出边界
					if (arr[j] < arr[j + 1]) {
						temp = arr[j];
						arr[j] = arr[j + 1];
						arr[j + 1] = temp;
					}
				}
			}
		}
		System.out.println();
		System.out.print("按从大到小的排序后:");
		System.out.println();
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + "  ");
		}
	}

	// 插入(将某值插在数组特定位置)
	public static void inset(int insetx, int index) {// 数组,插入值,插入位置
		int arrx[] = new int[arr.length + 1];
		for (int i = 0; i < arr.length; i++) {
			arrx[i] = arr[i];
		}
		System.out.println();
		for (int i = 0; i < arr.length; i++) {
			int temp;
			if (i == index - 1) {
				temp = arrx[i];
				arrx[i] = insetx;
				arrx[i + 1] = temp;

			}
			if (i > index - 1) {
				arrx[i + 1] = arr[i];
			}
		}
		System.out.print("插入(将某值插在数组特定位置):");
		System.out.println();
		for (int i = 0; i < arrx.length; i++) {
			System.out.print(arrx[i] + "  ");
		}
	}

	// 删除(将指定的值在数组里删除)
	public static void delete(int key) {
		int keyNum = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == key) {
				keyNum++;
			}
		}
		int arrx[] = new int[arr.length-keyNum];
		int j = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] != key) {
				arrx[j] = arr[i];
				j++;
			} 
		}
		System.out.println();
		System.out.print("删除(将指定的值在数组里删除)");
		System.out.println();
		for (int i = 0; i < arrx.length; i++) {
			System.out.print(arrx[i] + "  ");
		}
	}

	// 查找
	public static void find(int k) {
		System.out.println();
		boolean findit = false;// 判断是否找到
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == k) {
				System.out.println("查找到该值在" + "[" + i + "]" + "中");
				findit = true;
			}
		}
		if (findit == false) {
			System.out.println("数组中找不到该值!");
		}
	}

	public static void main(String[] args) {
		shiyan3_1_Array array = new shiyan3_1_Array();
		array.find(3);
		array.traverse();	
		array.sort();
		array.inset(10, 3);// 在array数组中第三个位置插入10
		array.delete(3);
	}
}

2. 将一个数组中的元素倒排过来,不能新开一个数组的临时存储空间,只能在原数组上改。

package shiyan3;

public class shiyan3_2 {
    public static int arr[] = { 5, 6, 1, 7, 2, 14, 2, 4};
	public static void Reverse() {
		System.out.print("倒排:");
		int temp;
		for (int i = 0; i < arr.length / 2 - 1; i++) {
			temp = arr[i];
			arr[i] = arr[arr.length - i - 1];
			arr[arr.length - i - 1] = temp;
		}

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

	public static void main(String[] args) {
        shiyan3_2 array=new shiyan3_2();
		array.Reverse();
	}
}

3. 实现在一个数组指定位置添加元素和删除元素的功能。

提示:解答该题需要考虑如下问题。

  1. 添加元素后超过数组容量时数组的扩展容量问题。
  2. 添加元素前后数组中元素的变化。
  3. 删除元素前后数组中元素的变化。
package shiyan3;

public class shiyan3_3 {
	public static int arr[] = { 4, 5, 6, 1, 7, 2, 4, 3 };
	// 插入(将某值插在数组特定位置)
	public static void inset(int insetx, int index) {// 数组,插入值,插入位置
		int arrx[] = new int[arr.length + 1];
		for (int i = 0; i < arr.length; i++) {
			arrx[i] = arr[i];
		}
		System.out.println();
		for (int i = 0; i < arr.length; i++) {
			int temp;
			if (i == index - 1) {
				temp = arrx[i];
				arrx[i] = insetx;
				arrx[i + 1] = temp;

			}
			if (i > index - 1) {
				arrx[i + 1] = arr[i];
			}
		}
		System.out.print("插入(将某值插在数组特定位置):");
		System.out.println();
		for (int i = 0; i < arrx.length; i++) {
			System.out.print(arrx[i] + "  ");
		}
	}
	// 删除(将数组特定位置的值删除)
	public static void delete(int index) {// 数组,插入值,插入位置
		int arrx[] = new int[arr.length - 1];
		for (int i = 0; i < arrx.length; i++) {
			if(i<index-1) {
				arrx[i] = arr[i];
			}
			else {
				arrx[i] = arr[i+1];
			}
		}
		System.out.println();
		System.out.println("删除(将数组特定位置的值删除)");
		for (int i = 0; i < arrx.length; i++) {
			System.out.print(arrx[i] + "  ");
		}
	}
	public static void main(String[] args) {
		shiyan3_3 arr=new shiyan3_3();
		arr.inset(10, 3);// 在array数组中第三个位置插入10
		arr.delete(3);//删除数组的第三个数
	}
}

这样就可以啦~拿去交差吧!

如果您觉得我写得不错,并且还觉得先支持一下的话,可以关注一下我的公众号哦~!

课程设计,实验报告需要帮忙的也可以哦!~

 

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值