Java基础--数组
数组概述
数组是相同类型数据的有序集合。
数组 描述的是相同类型的若干数据,按照一定的先后次序排列组合而成。
数组中的每一个数据称为一个元素,通过下标访问数组。
数组创建
package array;
public class Demo01 {
public static void main(String[] args) {
int[] numbers; //1.声明一个数组
numbers = new int[10]; //2.分配空间
//3. 赋值
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 each
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); //填充
//Arrays.fill(a,2,3,4);
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]));
}
//稀疏数组还原
//1. 读取稀疏数组的值
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();
}
}
}