5.2一维数组
-
数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同。
-
Java 语言中提供的数组是用来存储固定大小的同类型元素。
-
你可以声明一个数组变量,如 numbers[100] 来代替直接声明 100 个独立变量 number0,number1,....,number99。
package com.wkcto;
public class num {
public static void main(String[] args){
System.out.println("-------------第1种初始化方法---------------");
int a[] = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
System.out.println(a);
System.out.println("-------------第2种初始化方法---------------");
int b[] = new int[]{4,5,6,7,8,9};
System.out.println(b);
System.out.println("-------------第3种初始化方法---------------");
int c[] = {11,12,13,14,15};
for (int x:c) //foreach遍历数组
System.out.println(x); //输出遍历数组
}
}
//运行结果
-------------第1种初始化方法---------------
[I@1540e19d
-------------第2种初始化方法---------------
[I@677327b6
-------------第3种初始化方法---------------
11
12
13
14
15
Process finished with exit code 0
//实例
public class num {
public static void main(String[] args){
int a1[] = new int[0];
a1[0] = 1;
System.out.println(a1.length); //输出数组长度
}
}
//结论:数组长度new int 0,即便0这个下标(索引)也是不存在的,报错下标越界
//运行结果
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.wkcto.num.main(num.java:6)
Process finished with exit code 1
//实例
public class num {
public static void main(String[] args){
int a1[] = new int[2+4]; //将数组的长度赋予公式也是合法的,不可以把数组的长度设为小数,只能是公式或为整型常量
System.out.println(a1.length); //输出数组长度
}
}
//运行结果
6
//实例--创建数组,for循环返回每月有多少天
public class num {
public static void main(String[] args){
int day[] = {31,28,31,30,31,30,31,30,31,30,31};
for (int i = 0; i<day.length;i++){ //循环变量0;循环次数为day数组长度
System.out.println((i+1)+"月有"+day[i]+"天");
}
}
}
//运行结果
1月有31天
2月有28天
3月有31天
4月有30天
5月有31天
6月有30天
7月有31天
8月有30天
9月有31天
10月有30天
11月有31天
Process finished with exit code 0
5.3.二维数组
//实例--二维数组
public class num1 {
public static void main(String[] args) {
int arr[][] = {{1,2,3},{4,5,6}}; //第1种方法
int a2[][] = new int [][]{{1,2,3},{4,5,6}}; //第2种方法,
int a3[][] = new int[2][3]; //第3种方法
a3[0] = new int[]{1,2,3}; //给二维数组中第一个元素(第一行每一列)赋值
a3[1][0] = 56; //第一行第一列,分别给第二个元素每一列赋值
a3[1][1] = 11; //第一行第二列
a3[1][2] = 55; //第一行第三列
}
}
//实例---二维数组
/*
题干:创建一个二维数组,将古诗《春晓》的内容赋值于二维数组,然后分别用横版和竖版两种方式输出。
*/
public class num1 {
public static void main(String[] args) {
char arr[][] = new char[4][];
arr[0] = new char[]{'春','眠','不','觉','晓'};
arr[1] = new char[]{'处','处','闻','啼','鸟'};
arr[2] = new char[]{'夜','来','风','雨','声'};
arr[3] = new char[]{'花','落','知','多','少'};
System.out.println("-------横版输出-------");
for (int i=0; i<arr.length; i++){ //行:循环变量0,循环长度为arr数组长度,
for (int j=0; j<arr[i].length; j++){ //列:
System.out.print(arr[i][j]);
}
if (i%2==0){ //下标(索引)为0时%2等于0,为2时%2等于0
System.out.println(",");
}else{
System.out.println("。");
}
}
System.out.println("------竖版输出-------");
for (int j=0; j<arr[0].length; j++){
for (int i=3; i>=0; i--){ //i=3;i>=0是从后往前循环
System.out.print(arr[i][j]);
}
System.out.println();
}
System.out.println("。,。,"); //输出标点
}
}
//运行结果
-------横版输出-------
春眠不觉晓,
处处闻啼鸟。
夜来风雨声,
花落知多少。
------竖版输出-------
花夜处春
落来处眠
知风闻不
多雨啼觉
少声鸟晓
。,。,
Process finished with exit code 0
5.4.遍历数组
5.4.1.遍历数组
public class fill {
public static void main(String[] args) {
char arr[][] = new char[4][];
arr[0] = new char[]{'春', '眠', '不', '觉', '晓'};
arr[1] = new char[]{'处', '处', '闻', '啼', '鸟'};
arr[2] = new char[]{'夜', '来', '风', '雨', '声'};
arr[3] = new char[]{'花', '落', '知', '多', '少'};
for (char a[] : arr) { //循环变量:被遍历的数组集合
for (char b: a){
System.out.print(b);
}
System.out.println(); //每循环遍历出一行,输出一次换行符
}
}
}
//运行结果
春眠不觉晓
处处闻啼鸟
夜来风雨声
花落知多少
Process finished with exit code 0
5.4.2.填充替换数组元素
//实例---Arrays.fill填充
import java.util.Arrays;
public class fill {
public static void main(String[] args) {
int arr[] = new int[5];
//arr[0] = 7; //设arr一维数组下标0的位置值为7(结论:在填充前不应该有任何值存在,否则会被覆盖)
Arrays.fill(arr,10);
for (int i = 0; i <arr.length; i++){
System.out.println("第"+i+"个元素的值是:"+arr[i]);
}
}
}
//运行结果
第0个元素的值是:10
第1个元素的值是:10
第2个元素的值是:10
第3个元素的值是:10
第4个元素的值是:10
Process finished with exit code 0
//实例---Arrays.fill指定参数步值批量替换
public class fill {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8,9};
arr[0] = 7; //设arr一维数组下标0的位置值为7
Arrays.fill(arr,2,5,0); //如果取值范围也就是第三个参数大于数组长度,会报错数组下标越界
for (int i = 0; i <arr.length; i++){
System.out.println("第"+i+"个元素的值是:"+arr[i]);
}
}
}
//运行结果
第0个元素的值是:7
第1个元素的值是:2
第2个元素的值是:0
第3个元素的值是:0
第4个元素的值是:0
第5个元素的值是:6
第6个元素的值是:7
第7个元素的值是:8
第8个元素的值是:9
Process finished with exit code 0
//实例---Arrays.fill批量替换---手机号中间4位
public class fill {
public static void main(String[] args) {
int arr[] = {1,7,5,5,1,6,0,7,6,1,2};
Arrays.fill(arr,3,7,0); //在for循环前先进行替换,也就是这一句就开始替换
for (int i=0; i<arr.length; i++){ //循环变量0开始,循环次数小于数组arr长度,每次+1
if (arr[i] == 0){ //如果数组值等于0则输出*号
System.out.print("*");
}else{
System.out.print(arr[i]); //否则输出数字原值
}
}
}
}
//运行结果
175****7612
Process finished with exit code 0
5.4.3.对数组进行排序----Arrays.Sort方法
//实例---对数组进行排序Arrays.Sort
//优点:这种方法很简单,很短代码几乎可以实现类似“冒泡排序”“选择排序”的效果;
//缺点:只能升序排序,不能降序;不像冒泡和选择排序我们可以自定义排序结果。
import java.util.Arrays;
public class ArraySort1 {
public static void main(String[] args) {
int a[] = new int[]{56,14,92,1};
double b[] = new double[]{12.0,85.4,1.2,2.5};
Arrays.sort(a); //这种方法很简单,很短代码几乎可以实现类似“冒泡排序”“选择排序”的效果了
for (int tmp:a){ //循环变量:被循环数组集合(每次取一个值赋给循环变量)
System.out.print(tmp+" ");
}
System.out.println();
System.out.println("------------double浮点型------------");
Arrays.sort(b);
for (double tmp: b){
System.out.print(tmp+" ");
}
}
}
//运行结果
1 14 56 92
------------double浮点型------------
1.2 2.5 12.0 85.4
Process finished with exit code 0
6.1.面向对象
6.2.1.成员变量
6.2.2.成员方法
public class Demo {
int sum(){
int i = 0; //在for循环外层声明
for (i=0; i<10; i++){
if (i==3){
break; //这里写return也是一样的结果,因为当i等于3时强制中断
}
}
return i;
}
public static void main(String[] args) {
Demo d = new Demo();
int a =d.sum(); //调用d对象的sum方法
System.out.println(a); //输出a变量
}
}
//运行
3
Process finished with exit code 0
6.2.4.局部变量
//实例----不同区域的同名变量
public class Demo1 {
public void add(){
int id = 1; //声明局部变量,并进行初始化(赋值)
for (int i = 0; i < 100; i++){
System.out.print((i + id)+" ");
}
System.out.println("\n");
for (int i = 0; i < 100; i++){
System.out.print((i - id)+" "); //输出整型加字符串,因此需要连接符+,并给整型运算加括号
}
}
public static void main(String[] args){
Demo1 d = new Demo1(); //创建对象
d.add(); //调用d对象的add方法
}
}
//运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
Process finished with exit code 0
6.2.6.this关键字
//实例
public class EggCake {
int eggCount;
public EggCake(int eggCount){
this.eggCount = eggCount;
System.out.println("鸡蛋灌饼里有"+this.eggCount+"个鸡蛋");
}
//无参构造方法
public EggCake(){
this(1); //调用有参的构造方法
int i = 1; //在构造方法运行之前,不能进行初始化操作,因为构造方法本身就是初始化
}
public static void main(String[] args){
EggCake e1 = new EggCake(5); //创建对象,5个鸡蛋
EggCake e2 = new EggCake(); //创建对象,不赋值几个鸡蛋
}
}
//运行结果
鸡蛋灌饼里有5个鸡蛋
鸡蛋灌饼里有1个鸡蛋
Process finished with exit code 0
6.3.类的构造方法
6.4.1.静态变量
public class Demo3 {
static int x;
int y;
public Demo3(int x, int y){
this.x = x; //对象.类的成员变量=构造方法参数x
this.y = y;
}
public static void main(String[] args) {
Demo3 a = new Demo3(1,2);
Demo3 b = new Demo3(13,17);
System.out.println("a.x = " +a.x);
System.out.println("a.y = " +a.y);
System.out.println("a.x = " +b.x);
System.out.println("a.y = " +b.y);
}
}
//运行结果
a.x = 13
a.y = 2
a.x = 13
a.y = 17
Process finished with exit code 0
//实例---多个类共享同一个常量
public class Demo4 {
final static double PI = 3.1415926;
public static void main(String[] args) { //主方法
double radius = 3.0; //半径3.0
double area = Demo4.PI * radius * radius; // πr圆的平方,平方×两次
double volume = 4/3 * Demo4.PI * radius * radius * radius; //3分之4 πr的m³
Circular yuan = new Circular(radius,area); //创建对象
Sphereical qiu = new Sphereical(radius,volume); //创建对象
}
}
class Circular { //创建类
double radius; //创建类的成员变量
double area;
public Circular(double radius, double area){
this.radius = radius;
this.area = area;
System.out.println("圆的半径为"+this.radius+" 圆的面积是"+this.area);
}
}
class Sphereical{
double radius;
double valume;
public Sphereical(double radius, double valume){
this.radius = radius;
this.valume = valume;
System.out.println("球的半径为" + this.radius + " 球的体积是" + this.valume);
}
}
//运行结果
圆的半径为3.0 圆的面积是28.274333400000003
球的半径为3.0 球的体积是84.82300020000001
Process finished with exit code 0
6.4.2.静态方法
//实例---静态方法---
public class Demo5 {
public void show(){
System.out.println("Hello");
}
public static void main(String[] args){
Demo5 d = new Demo5();
d.show();
}
}
//运行结果
Hello
Process finished with exit code 0
//实例---静态方法---类中指定静态方法;主方法中直接类.方法 调用类的静态方法
public class Demo5 {
static public void show(){
System.out.println("Hello");
}
public static void main(String[] args){
Demo5.show(); //show字体是斜的说明是静态方法(调用静态方法不用创建对象)
}
}
//运行结果
Hello
Process finished with exit code 0
6.4.3.静态代码块
//静态代码块
public class Demo5{
static String name; //创建成员变量name
static {
System.out.println("这里是静态代码块"+name);
}
{
System.out.println("这里是非静态代码块"+name);
}
public Demo5(String name){
this.name = name;
System.out.println("这里是构造方法"+name);
}
public void show(){
System.out.println("这里是成员方法"+name);
}
public static void main(String[] args) {
Demo5 d = new Demo5("abc");
d.show();
}
}
//静态代码块在声明类对象的时候就会执行;而非静态代码块和构造方法则要创建对象后才执行
//运行结果