Java学习第二十二天<二维数组><逻辑数组><内嵌元素><杨辉三角形><习题><冒泡排序>
package chapter05.D2二维数组;
?
public class TwoDimensionalArray01 {
public static void main(String[] args) {
int [][]arr={{0,0,0,0,0,0},{0,0,1,0,0,0},{0,2,0,3,0,0},{0,0,0,0,0,0}};
System.out.println("二维数组元素个数="+arr.length);
for (int i = 0; i < arr.length ; i++) {//遍历二维数组每个元素(一维数组)
for (int j = 0; j System.out.print(arr[i][j]+" ");
}
System.out.println();//换行
}
?
}}
package chapter05.D2二维数组;
?
public class TwoDimensionalArray02 {
/*
二维数组
1
22
333
*/
public static void main(String[] args) {
int sum=0;
int [][]arr=new int[3][];//创建二维数组,但只确定了一维数组个数,3个,还没开数据空间
for (int i = 0; i < arr.length ; i++) {
?
arr[i]=new int[i+1];//给一维数组开空间 new
for (int j = 0; j < arr[i].length ; j++) {
arr[i][j]= i + 1;//遍历一维数组。给每个元素赋值
}
?
}
for (int i = 0; i for (int j = 0; j System.out.print(arr[i][j]+" ");
sum+=arr[i][j];
}
System.out.println();
}
System.out.println("sum="+sum);
}
}
布尔数组
package chapter05.D2二维数组;
?
public class BooleanArray {
public static void main(String[] args) {
String foo="blue";
boolean[] bar=new boolean[2];//开空间 bar[0] bar[1]默认false
if (bar[0]){
foo="green";
}
System.out.println(foo);
}
?
}
内嵌元素
package chapter05.D2二维数组;
?
public class Insert {
public static void main(String[] args) {
int arr[]={10,12,45,90};
int insertNum=23;
int index=-1;
for (int i = 0; i < arr.length ; i++) {
if (insertNum<=arr[i]){
index=i;
break;
}
}
if (index==-1){
index= args.length;
}//定位完成
int []arrNew=new int[arr.length+1];
for (int i = 0, j =0; i < arrNew.length ; i++) {
if (i!=index){
arrNew[i]=arr[j];
j++;
}else {
arrNew[i]=insertNum;
}
}
arr=arrNew;
for (int i = 0; i < arr.length ; i++) {
System.out.print(arr[i]+"\t");
?
}
}
}
杨辉三角形
package chapter05.D2二维数组;
?
public class YangHui {
public static void main(String[] args) {
int [][] yanghui=new int[10][];
?
for (int i = 0; i < yanghui.length ; i++) {//遍历yanghui每个元素
yanghui[i]=new int[i+1];//给每个一维数组开空间
for (int j = 0; j if (j==0||j==yanghui[i].length-1){
yanghui[i][j]=1;
}else {//中间元素
yanghui[i][j]=yanghui[i-1][j]+yanghui[i-1][j-1];
}
}
?
}
for (int i = 0; i < yanghui.length ; i++) {
for (int j = 0; j System.out.print(yanghui[i][j]+"\t");
}
System.out.println();
}
}
}
习题
package chapter05.D2二维数组;
?
public class Homework {
public static void main(String[] args) {
int []arr=new int[10];
for (int i = 0; i < arr.length ; i++) {
arr[i]=(int)(Math.random()*100+1);
?
}
System.out.println("===arr元素情况===");
for (int i = 0; i < arr.length ; i++) {
System.out.print(arr[i]+"\t");
}
System.out.println("\n====倒序打印====");
for (int i = arr.length-1; i >=0; i--) {
System.out.print(arr[i]+"\t");
}
double sum=arr[0];
int max=arr[0];
int maxIndex=0;
for (int i = 1; i < arr.length ; i++) {//从第一个数开始累计的
sum+=arr[i];
if (max max=arr[i];
maxIndex=i;
}
?
}
System.out.println("\nmax"+max+"maxIndex"+maxIndex);
System.out.println("\n平均值="+sum/arr.length);
int findNum=8;
int index=-1;
for (int i = 0; i < arr.length ; i++) {
if (findNum==arr[i]){
System.out.println("找到这个数");
index=1;
break;
}
}if (index==-1){
System.out.println("没找到"+findNum);
}
}
}
冒泡排序
package chapter05.D2二维数组;
?
public class Homework02 {
public static void main(String[] args) {
//冒泡排序
int []arr={20,-1,89,2,830,7};
int temp=0;
for (int i=0;i< arr.length-1;i++){
for (int j = 0; j < arr.length-1 ; j++) {
if (arr[j]>arr[j+1]){//从小到大>,大到小<
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("===排序后=====");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
?
}
}
}