总结之数组

一、数组

1.基本知识点

<类型>[] <数组名> = new <类型>[元素个数];

也可以直接写入数组里的元素。

例:int[] a={1,2,3,4};

int[] a = new int[10];//定义一个长度为10的a数组

2.默认初值:

public class Array {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		//默认初值
		 boolean bo[]=new boolean[2];//布尔类型
		byte b[]=new byte[2];//字节类型
		Boolean bo1[]=new Boolean[2];//布尔类型包装类
		Byte v[]=new Byte[2];//字节类型包装类
		System.out.println(bo[0]);//false
		System.out.println(bo1[0]);//null
		System.out.println(b[0]);//0
		System.out.println(v[0]);//null
}
}

char:'\u00000'

short/int:0

 long:0L

float:0.0f

double:0.0

3.输入一组数,输到-1结束,计算这一组数的平均值并输出比平均数大的数:

方法一:

import java.util.Scanner;

public class Array {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] number = new int[100];// 创建一组数组,数组长度我设的100,可按需设立
		double sum = 0.0;// 计算平均数,因为可能出现小数,所以我设的double类型
		int x = in.nextInt();// 输入一个值
		int i = 0;// 记录多少个数
		while (x != -1) {// 输入-1结束
			number[i] = x;// 对数组中的数字进行赋值
			sum += x;//求总和
			i++;
			x = in.nextInt();// 依次输入数字
		}
		if (i > 0) {
			double average = sum / i;// 设一个平均数
			System.out.println(average);// 输出平均数
			for (int j = 0; j < i; j++) {
				if (number[j] > average) {//找出大于平均值的数
					System.out.println(number[j]);
				}
			}
		}
	}
}

方法二:

import java.util.Scanner;

public class Array {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		double sum = 0.0;//设一个总数
		int count = in.nextInt();//输入多少个数
		int[] number = new int[count];//有多少数,设多大数组
		if (count > 0) {
			for (int i = 0; i < count; i++) {
				number[i] = in.nextInt();
				sum += number[i];
			}
			double average = sum / count;
			System.out.println("平均数是" + average);
			for (int i = 0; i < number.length; i++) {
				if (number[i] > average) {
					System.out.println(number[i]);
				}
			}
		}
	}
}

4.数组变量和复制数组

数组变量:

(1)是数组的管理者而非数组本身

(2)数组必须创建出来然后交给它来管理

(3)变量之间的赋值是管理权限的赋予

(4)变量之间的比较是判断是否管理同一个数组

public class Arrays {
	static void main(String[] args) {
		int[] a = new int[10];
		int[] b = a;
		a[0] = 5;
		System.out.println(a[0]);// 输出的是5
		b[0] = 16;
		System.out.println(a[0] + " " + b[0]);// 都是16

		int[] a1 = { 1, 2, 3, 4, 5 };
		int[] b1 = a1;
		System.out.println(a1 == b1);// a,b管理的同一个数组,输出true
		int[] b2 = { 1, 2, 3, 4, 5 };
		System.out.println(a1 == b2);// false
// 虽然赋的值是一样的,但是不是同一个数组(数组变量是数组的管理者而非数组本身)

		int[] a2 = a1;
		for (int i = 0; i < a2.length; i++) {
			a2[i]++;
		}
		for (int i = 0; i < a1.length; i++) {
			System.out.print(a1[i]);// 结果为 23456
		}
	}
}

 复制数组:

简单的方法:

public class Arrays {
	public static void main(String[] args) {
		// 复制数组
		int[] a = { 1, 2, 3, 4, 5 };
		int[] b = new int[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i] = a[i];
		}

		//比较两数组的值是否一样
		boolean isequ = true;
		for (int i = 0; i < b.length; i++) {
			if (a[i] != b[i]) {
				isequ = false;
				break;
			}
			System.out.println(b[i]);
		}
		System.out.println(a == b);//false
		System.out.println(isequ);//true
               
	}
}

其他的方法:

(1)import java.util.Arrays;Arrays.copyOf();

(2)

 

 

                int c[]=new int[10];
		int d[]=new int[c.length];
		for(int i=0;i<c.length;i++)
		{
			c[i]=i+1;
			System.arraycopy(c, 0, d, 0, 10);
			System.out.print(d[i]+" ");
		}

5.排序和去重

import java.util.Scanner;

public class Arrayslist {//不是ArrayList

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		System.out.println("请输入数组的元素:");
		int a[] = new int[8];//输入八个数
                /*也可以:int a=in.nextInt();
                int[] b=new int[a];
               */ 
                //循环初始化
		for (int i = 0; i < a.length; i++) {
			a[i] = in.nextInt();
			System.out.print(a[i] + " ");
		}
		System.out.println();
                //排序:从小到大
		for (int i = 0; i < a.length; i++) {
			for (int j = i + 1; j < a.length; j++) {
				if (a[i] > a[j]) {
					int n = a[i];
					a[i] = a[j];
					a[j] = n;
				}
			}
		}
		System.out.print("排序后:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
                //去重
		int index = 0;
		// 比较元素,将相同的元素删除
                // 1 2 2 3 4 5 5
		for (int i = 0; i < a.length; i++) {
			if (a[i] != a[index]) {
				a[++index] = a[i];
			}//可手动推算一下
		}
		System.out.print("去重后:");
		for (int i = 0; i < index + 1; i++) {
			System.out.print(a[i] + " ");
		}
	}
}

冒泡排序:

public class BubbleSort {
	public static void main(String[] args) {
		int[] a = { 1, 3, 2, 5, 6, 8, 4 };
		for (int i = 0; i < a.length; i++) {
			for (int j = i + 1; j < a.length; j++) {
				if (a[j] < a[i]) {
					int n = a[i];
					a[i] = a[j];
					a[j] = n;
				}
			}
		}
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}
}

6.在数组里面找某个数

import java.util.Scanner;

public class M {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int a[]={12,3,19,2,10,13,9,10};
		for(int i=0;i<a.length;i++)
		{
			if(n==a[i])
			{
				System.out.println("要查找的元素在第"+(i+1)+"个位置");
			}
		}
	}
}

7.打印字母

public class M{
	public static void main(String[] args){
		// 打印大、小写的英文字母
		char[] a = new char[26];
		for (int i = 0; i < 26; i++) {
			a[i] = (char) ('A' + i);
			System.out.print(a[i]+" ");
		}
		System.out.println();
		for (int i = 0; i < 26; i++) {

			a[i] = (char) ('a' + i);
			System.out.print(a[i]+" ");
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值