数组
1,数组概述
2,数组声明创建
package array;
public class Demo01 {
public static void main(String[] args) {
//求10个数的和;
int[] nums = new int[10];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
nums[5] = 6;
nums[6] = 7;
nums[7] = 8;
nums[8] = 9;
nums[9] = 10;
int sum=0;
for (int i=0;i<nums.length;i++) {
sum = sum + nums[i];
}
System.out.println("总和为:" + sum);
}
}
package array;
public class Demo02 {
public static void main(String[] args) {
int [] a = {1,2,3,4,5,6};
System.out.println(a[2]);
System.out.println("==========================");
int [] b = new int[5];
b[0] = 2;
b[1] = 3;
//如果没有定义初始值,数组会默认为0;
System.out.println(b[1]);
System.out.println(b[2]);
}
}
3,数组使用
package array;
public class Demo03 {
public static void main(String[] args) {
int [] array ={1,2,3,4,5,6};//输出数组
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println("==============");
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum = sum +array[i];
}
System.out.println("sum="+sum);//求数组的和
System.out.println("===============");
int max = array[0];
for (int i = 1; i < array.length; i++) {
if(max<array[i]){
max = array[i];
}
}
System.out.println("max="+max);//求数组最大值
}
}
package array;
public class Demo04 {
public static void main(String[] args) {
int [] array = {1,2,3,4,5,6};
for (int j : array) {
System.out.println(j);//增强式For循环,适用于打印东西,而且没有下标。
4,多维数组
package array;
public class Demo05 {
public static void main(String[] args) {
int [][] array = {{1,2},{3,4},{5,6}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
}
}
5,Arrays类
package array;
import java.util.Arrays;
public class Demo06 {
public static void main(String[] args) {
int [] a = {1,2,3,4,35,653,3423,52,42};
System.out.println(a);//[I@4eec7777
System.out.println( Arrays.toString(a));//输出数组
printArrays(a);//调用自己写的方法
Arrays.sort(a);//数组排序
System.out.println(Arrays.toString(a));
Arrays.fill(a, 2 ,4,0);//数组填充
System.out.println(Arrays.toString(a));
}
public static void printArrays(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 Demo07 {
public static void main(String[] args) {
int [] a = {1,3,4,7,6,23,43,13,645,67,88 };//从小到大排序;
sort(a);
System.out.println(Arrays.toString(a));
}
public static int [] sort(int [] a){
int temp = 0;
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j <a.length-1-i ; j++) {
if(a[j+1]>a[j]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
}
6,稀疏数组
package array;
public class Demo08 {
public static void main(String[] args) {
//创建一个二维数组10*10,
int [] [] a1 = new int [10] [10];
a1 [1][2]=6;
a1 [2][3]=8;
//输出二维数组;
System.out.println("输出原始数组");
for (int[] ints : a1) {
for(int anInt :ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println( "===================");
//转化为稀疏数组保存
//获取有效值得个数
int sum =0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(a1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值个数:"+sum);
//创建一个稀疏数组的数组;
int [][] a2 = new int[sum+1][3];
a2[0][0]=10;
a2[0][1]=10;
a2[0][2]=sum;
//遍历二维数组,将非零值放入稀疏数组中;
int count = 0;
for (int i = 0; i <a1.length ; i++) {
for (int j = 0; j < a1[i].length; j++) {
if (a1[i][j]!=0){
count++;
a2[count][0]=i;
a2[count][1]=j;
a2[count][2]=a1[i][j];
}
}
}
//输出稀疏数组
System.out.println("输出稀疏数组");
for (int i = 1; i < a2.length; i++) {
System.out.println(a2[i][0]+"\t"
+a2[i][1]+"\t"
+a2[i][2]+"\t");
}
System.out.println("=================");
System.out.println("还原");
//读取稀疏数组
int [][] a3 = new int[a2[0][0]][a2[0][1]];
//给其中的元素还原其中的值
for (int i = 1; i < a2.length; i++) {
a3[a2[i][0]][a2[i][1]]=a2[i][2];
}
//打印
System.out.println("输出还原数组");
for (int[] ints : a1) {
for(int anInt :ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
标签:int,System,数组,println,array,public,out
From: https://www.cnblogs.com/1802ming/p/17304193.html