Day17:一些数组练习题、简单面向对象练习

本文分享了关于数组的练习题解与简单的面向对象编程实践,适合初学者巩固基础知识。
摘要由CSDN通过智能技术生成

import java.util.Arrays;
import java.util.Scanner;

public class Demo05 {
	//3. 写一个函数,计算一个整数数组的平均值
//	public static void main(String[] args) {
//		int[] arr = { 1, 2, 3, 4, 5 };
//		getAvg(arr);
//	}
//	public static void getAvg(int[] arr) {
//		int sum = 0;
//		int avg = 0;
//		for(int i=0; i<arr.length; i++) {
//			System.out.print(arr[i] + "\t");
//			sum += arr[i];
//		}
//		System.out.println("\n和为:" + sum);
//		avg = sum / arr.length;
//		System.out.println("平均数为:" + avg);
//	}
	
	
	//4. 自定义一个整数数组 a,读入一个整数 n,如果 n 在数组中存在,则输出 n 的下标;如果 不存在,则输出-1。
//	public static void main(String[] args) {
//		int[] a = { 1, 2, 3, 4, 5 };
//		System.out.print("请输入一个整数:");
//		Scanner sc = new Scanner(System.in);
//		int n = sc.nextInt();
//		boolean flag = false;
//		for(int i=0; i<a.length; i++) {
//			if(n == a[i]) {
//				System.out.println("存在" + n + "下标为:" + i);
//				flag = true;
//				break;
//			}
//		}
//		if(!flag) {
//			System.out.println("不存在" + n + "下标为:-1");
//		}
//	}
	
	
	//5. 给定一个数组,输出数组中的最大值和最小值 
//	public static void main(String[] args) {
//		int[] a = { 11, 22, 1, 2, 3, 4, 5 };
//		int max = a[0];
//		int min = a[0];
//		for(int i=0; i<a.length; i++) {
//			if(min > a[i]) {
//				min = a[i];
//			}
//			if(max < a[i]) {
//				max = a[i];
//			}
//		}
//		System.out.println("最小值为:" + min);
//		System.out.println("最大值为:" + max);
//	}
	
	
	//6. 已知一个二维数组 A 表示一个矩阵,求 AT。 其中,AT 表示矩阵的转置。矩阵转置的义:表示把一个矩阵行列互换
//	public static void main(String[] args) {
//		
//	}
	
	
	//7. *给定一个数组,把这个数组中所有元素顺序进行颠倒。
//	public static void main(String[] args) {
//		int[] a = { 11, 22, 1, 2, 3, 4, 5 };
//		for (int i : a) {
//			System.out.print(i + "  ");
//		}
//		System.out.println();
//		for(int i=a.length-1; i>=0; i--) {
//			System.out.print(a[i] + "  ");
//		}
//	}
	
	
	//8. *数组的扩容。 给定一个数组,要求写一个 expand 函数,把原有数组的长度扩容一倍,并保留原有数 组原有的内容。
	//例如,给定一个数组 int[] a = {1,2,3},则扩容之后,a 数组为:{1,2,3,0,0,0} 
//	public static void main(String[] args) {
//		 int[] a = {1,2,3};
//		 for (int i : a) {
//			 System.out.print(i + "  ");
//		 }
//		 System.out.println();
//		 expend(a);
//		 
//	}
//	
//	public static void expend(int[] a) {
//		a = Arrays.copyOf(a, a.length*2);
		int[] newArray = new int[a.length * 2];   
		System.arraycopy(a, 0, newArray, 0, a.length);   
		a = newArray; 
//		for (int i : a) {
//			 System.out.print(i + "  ");
//		 }
//	}
	
	
	//9. *数组的插入和删除 写两个函数,一个函数为 delete 函数,声明如下: public static void delete(int pos) 
	//该函数表示删除数组 pos 位置上的元素。 
	//第二个函数为 insert 函数,声明如下: public static void insert(int pos, int value) 
	//该函数表示在数组 pos 位置上插入 value 值。
//	public static void main(String[] args) {
//		int[] a = {1,2,3,4,5};
//		int pos = 2;
//		int value = 8;
//		System.out.println("原数组:");
//		for (int i : a) {
//			System.out.print(i + " ");
//		}
//		System.out.println();
//		delete(a, pos);
//		insert(a, pos, value);
//	}
//	public static void delete(int[] a, int pos) {
//		int[] temp=new int[a.length-1];
//		System.arraycopy(a, 0, temp, 0, pos);
//		System.arraycopy(a, pos+1, temp, pos, temp.length-pos);
//		a=temp;
//		System.out.println("删除后:");
//		for (int i : a) {
//			System.out.print(i + " ");
//		}
//		System.out.println();
//	} 
//	public static void insert(int[] a, int pos, int value) {
//		a = Arrays.copyOf(a, a.length+1);
//		a[pos] = value;
//		System.out.println("插入后:");
//		for (int i : a) {
//			System.out.print(i + " ");
//		}
//	}
	
	static int[] a = { 1, 3, 2, 5, 7 }; // 多个函数可以直接操作该数组 
	static int index = a.length;
	static int[] arr;

	public static void main(String args[]) {
		delete(2); // 1 3 5 7
		insert(1, 4); // 1 4 3 5 7
		insert(0, 6); // 6 1 4 3 5 7
	}

	public static void delete(int pos){ 
		int[] temp=new int[a.length-1];
		System.arraycopy(a, 0, temp, 0, pos);
		System.arraycopy(a, pos+1, temp, pos, temp.length-pos);
		a=temp;
		System.out.println("删除后:");
		for (int i : a) {
			System.out.print(i + " ");
		}
		System.out.println(); 
	}

	public static void insert(int pos, int value){ 
		if(index == a.length) {
			a = Arrays.copyOf(a, a.length+1);
			System.arraycopy(a, 0, arr, 0, index);
		}else {
			arr = a;
		}
		System.arraycopy(arr, pos+1, arr, pos, index-pos);
		arr[pos] = value;
		index++;
		System.out.println("插入后:");
		for (int i : arr) {
			System.out.print(i + " ");
		} 
		System.out.println();
	}
}

public class Worker {
	private String name;
	private int age;
	private double salary;
	private Address addr;
	
	public Worker(String name, int age, double salary, Address addr) {
		this.name = name;
		this.age = age;
		this.salary = salary;
		this.addr = addr;
	}
	public Address getAddr() {
		return addr;
	}
	public void setAddr(Address addr) {
		this.addr = addr;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	public Worker() {
		System.out.println("无参构造");
	}
	public Worker(String name, int age, double salary) {
		this.name = name;
		this.age = age;
		this.salary = salary;
		System.out.println("有参构造");
	}
	
	public void work() {
		System.out.println("无参方法");
	}
	public void word(int hours) {
		System.out.println("有参方法\t工作" + hours + "小时");
	}
	
	
}

public class Address {
	private String address;
	private String zipCode;
	
	public Address() {
	}
	public Address(String address, String zipCode) {
		this.address = address;
		this.zipCode = zipCode;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	
	
}

public class Test {
	public static void main(String[] args) {
		Worker w1 = new Worker();
		Worker w2 = new Worker("zhangsan",25,2500);
		System.out.println("姓名:" + w2.getName() + ",年龄:" +w2.getAge() + ",薪资:" +w2.getSalary());
		w1.work();
		w1.word(12);
		Address a = new Address();
		a.setAddress("北京市海淀区清华园1号");
		a.setZipCode("100084");
		System.out.println("姓名:" + w2.getName() + ",年龄:" + w2.getAge() + ",工资:" + w2.getSalary()
				+",家庭住址为:" + a.getAddress() + ",邮政编码为:" + a.getZipCode());
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值