03 JAVA 数组

来源:互联网 发布:原生js ajax实例 编辑:程序博客网 时间:2024/06/08 15:48

一、数组定义

数组是多个相同类型数据集合(Python的列表支持不同类型的元素),我们可以对这些数据统一管理

- type varName[]

声明数组时不能指定其长度(就是先不指定数组中元素的个数,因为起先我们只是在栈中创建了varName的引用变量,而还未分配空间并给他赋值)varName是一个引用类型,指向堆中数据,数组可以被看成对象,数组中的每个元素相当于该对象的成员变量,数组元素可以是基本数据类型值或者引用类型存储reference

- 初始化

1. 动态初始化,先为数组分配空间(每个元素被隐式初始化,初始规则和类成员变量初始规则一样),之后为每个元素赋值

注意:方法返回值可以是数组引用

varName =  new type[size]

varName[index] = value


2. 静态初始化,定义数组的时候就分配空间

例如: Person p[] = {new Person("Amy"), new Person("Jack"), new Person("Cindy")}; 


二、arrayName.length获取数组的长度,我们可以使用index来获取数组元素,index的范围是[0, arrayName.length-1],下标可以是一个变量

三、args 一组字符串参数

java 程序名字 参数1,参数2,...

public class TestArgs {public static void main(String[] args) {int len = args.length;if(len < 3) {System.out.println("ARGUMENTS Format: \"NUM1\", \"OP\", \"NUM2\"");System.exit(-1);}try {double num1 = Double.parseDouble(args[0]);double num2 = Double.parseDouble(args[2]);double res = 0;if(args[1].equals("+"))res = num1 + num2;else if(args[1].equals("-")) res = num1 - num2;else if(args[1].equals("*")) res =  num1*num2;else if(args[1].equals("/")) res = num1/num2;else { System.out.println("Error format");System.exit(-1);}System.out.println(num1+args[1]+num2+"="+res);}catch(NumberFormatException e) {System.out.println(e);}}}

Additional: 

- 对象的比较,成员一位一位的比较

- 将数据从栈-》堆时,我们需要将基本数据包装成对象,比如可以将double类型数据包装成Double类对象,成员变量值是基本类型值

java.lang下面包装类有Double, Integer, Float等

parseDouble(参数),parseInt(参数)等都是静态方法

四、数组实例

1、数组排序

public class NumberSort {public static void main(String args[]) {NumberSort ns = new NumberSort();int len = args.length;int[] a = new int[len];for(int i = 0; i < len; i++){a[i]=Integer.parseInt(args[i]);}ns.print(ns.bubbleSort(a));}void print(int[] a) {int len = a.length;for(int i = 0; i < len; i++) {System.out.print(a[i]+" ");}}int[] selectionSort(int[] a) {int tmp = 0, k = 0, len = a.length;for(int i = 0; i < len-1; i++) {k = i;for(int j = i; j < len; j++) {if(a[k] > a[j]) {k = j;}}if(k != i) {tmp = a[i];a[i] = a[k];a[k] = tmp;}}return a;}int[] bubbleSort(int[] a) {int tmp = 0, len = a.length;for(int i = len-1; i > 0; i--) {for(int j = 0; j < i; j++) {if(a[j] > a[j+1]) {tmp = a[j];a[j] = a[j+1];a[j+1] = tmp;}}}return a;}}
2、如果有500个人手拉手,并且数三退一,最后留下的那个人是排在第几位?

import java.util.*;public class ThreeforQuit {public static void main(String[] args) { Scanner ns =  new Scanner(System.in);int len = ns.nextInt(), tmp = len, count = 0, idx = 0;boolean[] node = new boolean[len];for(int i = 0; i < len; i++) {node[i] = true;}while(tmp > 1) {if(node[idx]) {count++;if(count == 3) {count = 0;node[idx] = false;tmp--;}}idx++;if(idx == len) idx = 0;}print(node);}public static void print(boolean[] node) {for(int i = 0; i < node.length; i++) {if(node[i]) {System.out.println(i+1);break;}}}}

class Node1 {int id;Node1 previous, next;Node1(int id) {this.id = id;}}class LinkedCircle {int cid = 1;Node1 first, last;LinkedCircle(int n) {while (cid <= n) {Node1 tmp =new Node1(cid);if(cid == 1) {first = last = tmp;}else {last.next = tmp;tmp.previous = last;tmp.next = first;first.previous = tmp;last = tmp;}cid++;}}void delete(Node1 node1) {if(cid == 1) {first = last = null;}else {node1.previous.next = node1.next;node1.next.previous = node1.previous;if(node1 == first) first = first.next;else if(node1 == last)last = last.previous;}cid--;}}public class ThreeforQuit3 {public static void main(String args[]) {LinkedCircle lc = new LinkedCircle(500);Node1 node1 = lc.first;int countNum = 0;while(lc.cid > 1) {countNum++;if(countNum == 3) {countNum = 0;lc.delete(node1);}node1 = node1.next;}System.out.println(lc.first.id);}}

3. 二分法查找

import java.util.*;public class BinarySearch {public static void main(String args[]) {int[] a = new int[args.length];for(int i = 0; i < args.length; i++) {a[i] = Integer.parseInt(args[i]);}Scanner sn = new Scanner(System.in);int obj = sn.nextInt();sn.close();System.out.print(binarySearch(a, obj));}static int binarySearch(int[] a, int obj) {if (a.length == 0) return -1;int head = 0, tail = a.length - 1;int mid = (head + tail) / 2;while(head <= tail) {if(a[mid] == obj) {//2,3 2是第一位,3是第二位return mid + 1;}else if(a[mid] < obj) {head = mid + 1;}elsetail = mid-1;mid = (tail + head) / 2;}return -1;}}
4. 数组的复制和二维数组

class Dot {int id;Dot(int id) {this.id = id;}}public class testCopy {static void copyInt() {int[] a = {1, 2, 3};int[] b = new int[a.length];int[] c = new int[a.length];for(int i = 0; i < a.length; i++) {b[i] = a[i];}System.arraycopy(a, 0, c, 0, a.length);a[1] = 9;System.out.println("a[1]:"+a[1]+" b[1]:"+b[1]+" c[1]:"+c[1]);}static void copyHeapObj() {Dot[] a = {new Dot(1), new Dot(2), new Dot(3)};Dot[] b = new Dot[a.length];Dot[] c = new Dot[a.length];Dot[] d = new Dot[a.length];for(int i = 0; i < a.length; i++) {b[i] = a[i];}for(int i = 0; i < a.length; i++) {d[i] = new Dot(a[i].id);}System.arraycopy(a, 0, c, 0, a.length);a[1].id = 9;System.out.println("a[1]:"+a[1].id+" b[1]:"+b[1].id+" c[1]:"+c[1].id+" d[1]:"+d[1].id);}static void copyString() {String[] a = {"aa", "bb", "cc"};String[] b = new String[a.length];String[] c = new String[a.length];for(int i = 0; i < a.length; i++) {b[i] = a[i];}System.arraycopy(a, 0, c, 0, a.length);a[1] = "dd";System.out.println("a[1]:"+a[1]+" b[1]:"+b[1]+" c[1]:"+c[1]);}public static void main(String args[]) {copyInt();copyString();copyHeapObj();}}
结果:

a[1]:9 b[1]:2 c[1]:2

a[1]:dd b[1]:bb c[1]:bb

a[1]:9 b[1]:9 c[1]:9 d[1]:2

public class TwoDimension {public static void main(String args[]) {int[][] intArray = {{1,2}, {1,2,3}, {3,4}};int[][] int1 = new int[3][];int[][] int2 = new int[3][];int[][] int3 = new int[3][];for(int i = 0; i < intArray.length; i++) {int1[i] = new int[intArray[i].length]; for(int j = 0; j < intArray[i].length; j++) {int1[i][j] = intArray[i][j];}}for(int i = 0; i < intArray.length; i++) {int3[i] = intArray[i]; }System.arraycopy(intArray, 0, int2, 0, intArray.length);intArray[2][1] = 100;System.out.println(intArray[2][1]+","+int1[2][1]+","+int2[2][1]+","+int3[2][1]);}}
结果:

100,4,100,100


Reference:

1、马士兵JAVA基础视频

0 0