数组
数组的定义
数组的声明和创建
package array;
//数组的声明和创建
public class Demo01 {
public static void main(String[] args) {
int[] nums;//声明一个数组
nums = new int[10];//创建一个数组
//给数组分配值
for (int i=0 ; i<nums.length ;i++ ){
nums[i]=i*10;
}
System.out.println(nums[0]+" "+nums[9]);
}
}
三种初始化及内存分析
package array;
//三种初始化及内存分析
public class Demo02 {
public static void main(String[] args) {
//静态初始化
int[] a={1,2,3,4,5};
System.out.println(a[0]);
//动态初始化:包含默认初始化
int[] b = new int[5];
b[0] = 0;
System.out.println(b[0]+" "+b[4]);
}
}
数组的四个基本特点
数组的使用
package array;
//数组的使用
public class Demo03 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
//打印全部元素
for (int i=0 ; i<arrays.length ; i++){
System.out.print(arrays[i]+" ");
}
System.out.println();
//计算所有元素总和
int sum=0;
for (int i=0 ; i<arrays.length ; i++){
sum+=arrays[i];
}
System.out.println("sum="+sum);
//查找最大元素
int max=arrays[0];
for (int array : arrays) {
if(array>max){
max=array;
}
}
System.out.println("max="+max);
//打印反转元素
int[] reverse = reverse(arrays);
for (int i : reverse) {
System.out.print(i+" ");
}
System.out.println();
}
//反转数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
//反转
for (int i=0 ; i< result.length ; i++){
result[i] = arrays[arrays.length-1-i];
}
return result;
}
}
多维数组
package array;
//多维数组
public class Demo04 {
public static void main(String[] args) {
int[][] array = {{1,1},{2,2},{3,3}};
for (int[] ints : array) {
for (int anInt : ints) {
System.out.println(anInt);
}
}
}
}
Arrays讲解
package array;
//Arrays讲解
import java.util.Arrays;
import java.util.Collections;
//Arrays类讲解
public class Demo05 {
public static void main(String[] args) {
int[] a = {1,2,15,773,534,35734,3,5,643};
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));
}
}
冒泡排序和选择排序
package array;
import java.util.Arrays;
//冒泡排序和选择排序
public class Demo06 {
public static void main(String[] args) {
int[] a = {1,2,15,773,534,35734,3,5,643};
int[] b = {1,2,15,773,534,35734,3,5,643};
//冒泡排序
int temp;
//外层循环,控制比较的轮数
for (int i = 0 ; i < a.length-1 ; i++) {
for (int j = 0 ; j < a.length-1-i ; j++) {//每轮比较的次数,每经历一轮比较的个数少一个
if(a[j] > a[j+1]){//比较相邻的两个数的大小
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
//选择排序
for (int i = 0 ; i < b.length-1 ; i++) {
for (int j = i+1 ; j < b.length ; j++) {
if(b[j] < b[i]){
temp = b[j];
b[j] = b[i];
b[i] = temp;
}
}
}
System.out.println(Arrays.toString(b));
}
}
稀疏数组
package array;
import java.lang.reflect.Array;
//稀疏数组
public class Demo07 {
public static void main(String[] args) {
//创建一个数组11*11 0:没有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//输出原始数组
System.out.println("原始数组");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+" ");
}
System.out.println();
}
System.out.println("---------------------");
//转化为稀疏数组保存
//获取有效值的个数
int sum=0;
for (int[] ints : array1) {
for (int anInt : ints) {
if(anInt != 0){
sum++;
}
}
}
//创建一个稀疏数组
int[][] array2 = new int[sum+1][3];
array2[0][0] = 11;
array2[0][1] = 11;
array2[0][2] = sum;
//遍历二维数组,将非0的值存放稀疏数组中
int count=0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if(array1[i][j] != 0){
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array1[i][j];
}
}
}
//打印稀疏数组
System.out.println("稀疏数组");
for (int[] ints : array2) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.println();
}
System.out.println("---------------------");
//还原稀疏数组
int[][] array3 = new int[array2[0][0]][array2[0][1]];
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
//输出还原数组
System.out.println("还原数组");
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt+" ");
}
System.out.println();
}
}
}
标签:__,int,Day06,System,数组,println,public,out
From: https://www.cnblogs.com/lyc2001/p/16657709.html