数组概述
数组是相同类型数据的有序集合。 数组 描述的是相同类型的若干数据,按照一定的先后次序排列组合而成。 数组中的每一个数据称为一个元素,通过下标访问数组。
数组创建
package array
;
public class Demo01 {
public static void main(String
[] args
) {
int[] numbers
;
numbers
= new int[10];
numbers
[0] = 1;
numbers
[1] = 2;
numbers
[2] = 3;
numbers
[3] = 4;
numbers
[4] = 5;
numbers
[5] = 6;
numbers
[6] = 7;
numbers
[7] = 8;
numbers
[8] = 9;
numbers
[9] = 10;
System
.out
.println(numbers
[2]);
int sum
= 0;
for (int i
= 0; i
<numbers
.length
; i
++) {
sum
+= numbers
[i
];
}
System
.out
.println("总和为:" + sum
);
}
}
package array
;
public class Demo02 {
public static void main(String
[] args
) {
int[] a
= {1,2,3,4,5};
Man
[] man
= {new Man(),new Man()};
int[] b
= new int[10];
b
[0] = 10;
System
.out
.println(b
[0]);
System
.out
.println(b
[1]);
}
}
数组使用
package array
;
import java
.sql
.PreparedStatement
;
public class Demo03 {
public static void main(String
[] args
) {
int[] array
= {1,2,3,4,5};
for (int i
= 0; i
< array
.length
; i
++) {
System
.out
.println(array
[i
]);
}
int sum
= 0;
for (int i
= 0; i
< array
.length
; i
++) {
sum
+= array
[i
];
}
System
.out
.println(sum
);
int max
= array
[0];
for (int i
= 0; i
<array
.length
; i
++) {
if(array
[i
] > max
){
max
= array
[i
];
}
}
System
.out
.println(max
);
for (int arr
:array
) {
System
.out
.println(arr
);
}
int[] result
= new int[array
.length
];
for (int i
= 0, j
= array
.length
-1; i
< array
.length
; i
++,j
--) {
result
[i
] = array
[j
];
}
for (int res
:result
) {
System
.out
.println(res
);
}
}
}
多维数组
package array
;
public class Demo04 {
public static void main(String
[] args
) {
int[][] arrays
= {{1,2,3},{4,5,6}};
for (int i
= 0; i
< 2 ; i
++) {
for (int j
= 0; j
<arrays
[0].length
; j
++) {
System
.out
.println(arrays
[i
][j
]);
}
}
}
}
Arrays类
package array
;
import java
.util
.Arrays
;
public class ArraysDemo {
public static void main(String
[] args
) {
int[] a
= {1,2,5,3,343,56,43,45454};
System
.out
.println(Arrays
.toString(a
));
Arrays
.sort(a
);
System
.out
.println(Arrays
.toString(a
));
Arrays
.fill(a
,0);
System
.out
.println(Arrays
.toString(a
));
}
public static void PrintArray(int[] a
){
for (int i
= 0; i
<a
.length
; i
++) {
if (i
== 0) {
System
.out
.print("[");
}
if (i
== a
.length
- 1) {
System
.out
.print(a
[i
] + "]");
} else {
System
.out
.print(a
[i
] + ", ");
}
}
}
}
冒泡排序
package array
;
import java
.util
.Arrays
;
public class BubbleSort {
public static void main(String
[] args
) {
int[] a
= {1,3,2,4,9,6,5,7,8};
System
.out
.println(Arrays
.toString(a
));
int[] sort
= bubleSort(a
);
System
.out
.println(Arrays
.toString(sort
));
}
public static int[] bubleSort(int[] a
){
for (int i
= 0; i
<a
.length
-1 ; i
++) {
boolean flag
= false;
for (int j
= 0; j
<a
.length
-1-i
; j
++) {
if(a
[j
]>a
[j
+1]){
int t
= a
[j
];
a
[j
] = a
[j
+1];
a
[j
+1] = t
;
flag
= true;
}
}
if(flag
==false){
break;
}
}
return a
;
}
}
稀疏数组
package array
;
import java
.util
.Arrays
;
public class xisushuzu {
public static void main(String
[] args
) {
int[][] a
= new int[11][11];
a
[1][2] = 1;
a
[2][3] =2;
System
.out
.println("原始数组:");
for (int[] arr
:a
) {
for (int ar
:arr
) {
System
.out
.print(ar
+"\t");
}
System
.out
.println();
}
int count
=0;
for (int i
= 0; i
< a
.length
; i
++) {
for (int j
= 0; j
<a
[i
].length
; j
++) {
if(a
[i
][j
]!=0){
count
++;
}
}
}
int[][] xisu
= new int[count
+1][3];
xisu
[0][0] = a
.length
;
xisu
[0][1] = a
[0].length
;
xisu
[0][2] = count
;
int k
= 1;
for (int i
= 0; i
< a
.length
; i
++) {
for (int j
= 0; j
<a
[i
].length
; j
++) {
if(a
[i
][j
]!=0){
xisu
[k
][0] = i
;
xisu
[k
][1] = j
;
xisu
[k
][2] = a
[i
][j
];
k
++;
}
}
}
System
.out
.println("稀疏数组:");
for (int i
= 0; i
<= count
; i
++) {
System
.out
.println(Arrays
.toString(xisu
[i
]));
}
int[][] arrr
= new int[xisu
[0][0]][xisu
[0][1]];
for (int i
= 1; i
<xisu
.length
; i
++) {
arrr
[xisu
[i
][0]][xisu
[i
][1]] = xisu
[i
][2];
}
System
.out
.println("还原数组:");
for (int[] br
:arrr
) {
for (int b
:br
) {
System
.out
.print(b
+"\t");
}
System
.out
.println();
}
}
}
<br /
转载请注明原文地址:https://tech.qufami.com/read-18447.html