声明格式:
//type为数组类型,var为数组名
type var[];
type[] var;
//例如:
int a1[];int[] a2;
double b[];
Person[] p1;String s1[];
Java语言中声明数组时不能指定其长度(数组中元素的个数),例如:
int a[5]; //非法
Java中使用关键字new创建数组对象,格式为:
var = new type[数组元素的个数]
public class Test {
public static void main(String args[]){
int[] s;
s = new int[5];
System.out.println(s[1]);//输出:0
//为数组元素赋值,索引从0开始
for (int i = 0;i<5;i++){
s[i] = i;
}
System.out.println(s[1]);//输出:1
}
}
元素为引用数据类型的数组中的每一个元素都需要实例化
public class Test {
public static void main(String args[]){
Date[] days;
days = new Date[3];
System.out.println(days[1]);//输出:null
for (int i = 0;i<3;i++){
days[i] = new Date(2020,7,1+i);
}
System.out.println(days[1].day);//输出:2
}
}
class Date{
int year;int month;int day;
Date(int y,int m,int d){
year = y;
month = m;
day = d;
}
}
public class Test {
public static void main(String args[]){
int[] s;//定义数组
s = new int[3];//分配空间
s[0]=3;s[1]=9;s[2]=8;//赋值
Date[] days;
days = new Date[3];
days[0] = new Date(2023,7,0);
days[1] = new Date(2023,7,1);
days[2] = new Date(2023,7,2);
}
}
class Date{
int year;int month;int day;
Date(int y,int m,int d){
year = y;
month = m;
day = d;
}
}
public class Test {
public static void main(String args[]){
int[] s = {3,9,8};
int[] s1 = new int[]{3,9,8};//完整格式
Date[] days = new Date{
days[0] = new Date(2023,7,0),
days[1] = new Date(2023,7,1),
days[2] = new Date(2023,7,2)
};
}
}
class Date{
int year;int month;int day;
Date(int y,int m,int d){
year = y;
month = m;
day = d;
}
}
注:默认初始化
数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐式初始化。
s.length //数组s的长度(数组s的元素个数)
package Array;
public class TestArray {
public static void main(String[] args) {
int[] a = {2, 4, 6, 7, 3, 5, 1, 9, 8};
/*
for(int i=0; i
for(int i=0; i<args.length; i++) {
System.out.println(args[i]);
}
}
}
package Array;
public class TestArgs {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println(
"Usage: java Test \"n1\" \"op\" \"n2\"");
System.exit(-1);
}
double d1 = Double.parseDouble(args[0]); //String类型强制转换为Double类型
double d2 = Double.parseDouble(args[2]);
double d = 0;
if (args[1].equals("+")) d = d1 + d2;
else if (args[1].equals("-")) d = d1 - d2;
else if (args[1].equals("x")) d = d1 * d2;
else if (args[1].equals("/")) d = d1 / d2;
else {
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(d);
}
}
要求:依靠args读入2, 4, 6, 7, 3, 5, 1, 9, 8;并按从小到大排序
package Array;
public class NumSort {
public static void main(String[] args) {
int[] a = new int[args.length];
//String转换成int
for (int i=0; i<args.length; i++) {
a[i] = Integer.parseInt(args[i]);
}
print(a);
selectionSort(a);
print(a);
}
//排序
private static void selectionSort(int[] a) {
for(int i=0; i<a.length; i++) {
for(int j=i+1; j<a.length; j++) {
//每次找到更小值都交换
if(a[j] < a[i]) {
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
//排序(效率更高,每次只做一次交换)
private static void selectionSort2(int[] a) {
int k, temp;
for(int i=0; i<a.length; i++) {
k = i;
//找到最小值的索引
for(int j=k+1; j<a.length; j++) {
if(a[j] < a[k]) {
k = j;
}
}
//交换最小值与当前值
if(k != i) {
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
//将数组打印出来
private static void print(int[] a) {
for(int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
package Array;
public class TestDateSort {
public static void main(String[] args) {
Date[] days = new Date[5];
days[0] = new Date(2006, 5, 4);
days[1] = new Date(2006, 7, 4);
days[2] = new Date(2008, 5, 4);
days[3] = new Date(2004, 5, 9);
days[4] = new Date(2004, 5, 4);
Date d = new Date(2006, 7, 4);
String str = String.valueOf(d);
//str = d.toString();
bubbleSort(days);
for (int i = 0; i < days.length; i++) {
System.out.println(days[i]);
}
System.out.println(binarySearch(days, d));
}
//冒泡排序:每次循环将最大的值挪到最后
public static Date[] bubbleSort(Date[] a) {
int len = a.length;
for (int i = len - 1; i >= 1; i--) {
for (int j = 0; j <= i - 1; j++) {
if (a[j].compare(a[j + 1]) > 0) {
Date temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
return a;
}
//二分法查找
public static int binarySearch(Date[] days, Date d) {
if (days.length == 0) return -1;
int startPos = 0;
int endPos = days.length - 1;
int m = (startPos + endPos) / 2;
while (startPos <= endPos) {
if (d.compare(days[m]) == 0) return m;
if (d.compare(days[m]) > 0) {
startPos = m + 1;
}
if (d.compare(days[m]) < 0) {
endPos = m - 1;
}
m = (startPos + endPos) / 2;
}
return -1;
}
}
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
//比较年月日大小
public int compare(Date date) {
return year > date.year ? 1
: year < date.year ? -1
: month > date.month ? 1
: month < date.month ? -1
: day > date.day ? 1
: day < date.day ? -1 : 0;
}
//重写toString方法,否则返回的是类名和它的引用地址
public String toString() {
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
第一种
package Array;
public class Count3Quit {
public static void main(String[] args) {
boolean[] arr = new boolean[500];
for(int i=0; i<arr.length; i++) {
arr[i] = true;
}
int leftCount = arr.length;
int countNum = 0;
int index = 0;
while(leftCount > 1) {
if(arr[index] == true) {
countNum ++;
if(countNum == 3) {
countNum = 0;
arr[index] = false;
leftCount --;
}
}
index ++;
if(index == arr.length) {
index = 0;
}
}
for(int i=0; i<arr.length; i++) {
if(arr[i] == true) {
System.out.println(i);
}
}
}
}
第二种
package Array;
public class Count3Quit2 {
public static void main(String[] args) {
KidCircle kc = new KidCircle(500);
int countNum = 0;
Kid k = kc.first;
while(kc.count > 1) {
countNum ++;
if(countNum == 3) {
countNum = 0;
kc.delete(k);
}
k = k.right;
}
System.out.println(kc.first.id);
}
}
class Kid {
int id;
Kid left;
Kid right;
}
class KidCircle {
int count = 0;
Kid first, last;
KidCircle(int n) {
for(int i=0; i<n; i++) {
add();
}
}
void add() {
Kid k = new Kid();
k.id = count;
if(count <= 0) {
first = k;
last = k;
k.left = k;
k.right = k;
} else {
last.right = k;
k.left = last;
k.right = first;
first.left = k;
last = k;
}
count ++;
}
void delete(Kid k) {
if(count <= 0) {
return;
} else if (count == 1) {
first = last = null;
} else {
k.left.right = k.right;
k.right.left = k.left;
if(k == first) {
first = k.right;
} else if( k == last) {
last = k.left;
}
}
count --;
}
}
package Array;
public class TestSearch {
public static void main(String[] args) {
int a[] = {1, 3, 6, 8, 9, 10, 12, 18, 20, 34};
int i = 12;
//System.out.println(search(a, i));
System.out.println(binarySearch(a, i));
}
public static int search(int[] a, int num) {
for (int i = 0; i < a.length; i++) {
if (a[i] == num) return i;
}
return -1;
}
//二分法查找
public static int binarySearch(int[] a, int num) {
if (a.length == 0) return -1;
int startPos = 0;
int endPos = a.length - 1;
int m = (startPos + endPos) / 2;
while (startPos <= endPos) {
if (num == a[m]) return m;
if (num > a[m]) {
startPos = m + 1;
}
if (num < a[m]) {
endPos = m - 1;
}
m = (startPos + endPos) / 2;
}
return -1;
}
}
int a[][] ={{1,2},{3,4,5,6},{7,8,9}};
int a[][] = new int[3][];
a[0] = new int[2];
a[1] = nwe int[4];
a[2] = nwe int[3];
int t[][] = new int[][4];//非法
int intA[][] = {{1,2},{2,3},{3,4,5}};
int intA[3][2] = {{1,2},{2,3},{4,5}};//非法
int a[][] = new int[3][5];
int b[][] = new int[3][];
b[0] = new int[2];
b[1] = nwe int[3];
b[2] = nwe int[5];
public static void arraycopy(Object src,int srcPos,Object destint,destPos,int length)
package Array;
public class TestArrayCopy {
public static void main(String args[]) {
String[] s =
{"Mircosoft","IBM","Sun","Oracle","Apple"};
String[] sBak = new String[6];
System.arraycopy(s,0,sBak,0,s.length);
for(int i=0;i<sBak.length;i++){
System.out.print(sBak[i]+" ");
}
System.out.println();
int[][] intArray = {{1,2},{1,2,3},{3,4}};
int[][] intArrayBak = new int[3][];
System.arraycopy
(intArray,0,intArrayBak,0,intArray.length);
intArrayBak[2][1] = 100;
for(int i = 0;i<intArray.length;i++){
for(int j =0;j<intArray[i].length;j++){
System.out.print(intArray[i][j]+" ");
}
System.out.println();
}
}
}