黄冈论坛网站有哪些,品牌型网站制作哪,安联建设集团股份公司网站,广州安全教育平台入口登录官网一、数组
#xff08;一#xff09;数组的使用
1、使用方式1——动态初始化
2、使用方式2——动态初始化
3、使用方式3——静态初始化
#xff08;二#xff09;数组使用注意事项和细节
#xff08;三#xff09;数组赋值机制
#xff08;四#xff09;数组拷贝…一、数组
一数组的使用
1、使用方式1——动态初始化
2、使用方式2——动态初始化
3、使用方式3——静态初始化
二数组使用注意事项和细节
三数组赋值机制
四数组拷贝
五数组反转
六数组扩容
七数组缩减
二、排序
冒泡排序
三、查找
顺序查找
四、多维数组——二维数组
一二维数组的使用
1、使用方式1——动态初始化 2、使用方式2——动态初始化
3、使用方式3——动态初始化——列数不确定
4、使用方式4——静态初始化
二二维数组使用注意事项和细节
一、数组
数组可以存放多个同一类型的数据。数组也是一种数据类型是引用类型。即数组就是一组数据。
一数组的使用
1、使用方式1——动态初始化
1数组的定义 数据类型 数组名[] new 数据类型[大小] 或 数据类型[] 数组名 new 数据类型[大小] int a[] new int[5];
2数组的引用使用数组名[下标/索引/index]
import java.util.Scanner;
public class Array02{public static void main(String[] args){Scanner myScanner new Scanner(System.in);double scores[] new double[5];for(int i 0; i scores.length; i){System.out.print(请输入第 (i 1) 个元素的值);scores[i] myScanner.nextDouble();}System.out.println(当前数组的值如下:);for(int i 0; i scores.length; i){System.out.print(scores[i] );}}
}
2、使用方式2——动态初始化
1先声明数组 数据类型 数组名[]; 或 数据类型[] 数组名; int a[];
2创建数组 数组名 new 数据类型[大小]; a new int[10];
import java.util.Scanner;
public class Array03{public static void main(String[] args){Scanner myScanner new Scanner(System.in);double scores[]; //声明数组这时scores是nullscores new double[5]; // 分配内存空间可以存放数据for(int i 0; i scores.length; i){System.out.print(请输入第 (i 1) 个元素的值);scores[i] myScanner.nextDouble();}System.out.println(当前数组的值如下:);for(int i 0; i scores.length; i){System.out.print(scores[i] );}}
}
3、使用方式3——静态初始化
1初始化数组 数据类型 数组名[] {元素值,元素值...} int a[] {2,5,6,7,8,89,90,34,56} 上面的用法相当于int a[] new int[9]; a[0] 2; a[1] 5; a[2] 6; a[3] 7; a[4] 8; a[5] 89; a[6] 90; a[7] 34; a[8] 56; public class Array01{public static void main(String[] args){double hens[] {3, 5, 1, 3.4, 2, 50};double sum 0;for(int i 0; i hens.length; i){sum hens[i];}System.out.println(sum sum ,avg sum/hens.length);}
}
二数组使用注意事项和细节
1、数组是多个相同类型数据的组合实现对这些数据的统一管理。
2、数组中的元素可以是任何数据类型包括基本类型和引用类型但是不能混用。 3、数组创建后如果没有赋值有默认值。int 0,short 0,byte 0,long 0,float 0.0,double 0.0,
char \u0000,boolean false,String null
4、使用数组的步骤1声明数组并开辟空间 2给数组各个元素赋值 3使用数组
5、数组的下标是从0开始的。
6、数组下标必须在指定范围内使用否则报下标越界异常比如int arr[] new int[5];的有效下标为0-4。
7、数组属于引用类型数组型数据是对象object
三数组赋值机制
1、基本数据类型赋值这个值就是具体的数据而且相互不影响。值传递/值拷贝 2、数组在默认情况下是引用传递赋的值是地址。
public class Array06{public static void main(String[] args){int arr1[] {1,2,3,4,5};int arr2[] arr1;arr2[0] 10;for(int i 0; i arr1.length; i){System.out.print(arr1[i] );}}
} 四数组拷贝
将arr1拷贝到arr2要求数据空间是独立的。
public class Array07{public static void main(String[] args){int arr1[] {1,2,3};// 开辟一个和arr1一样大的数组int arr2[] new int[arr1.length];for(int i 0; i arr1.length; i){arr2[i] arr1[i];}// 修改arr2[0]arr2[0] 10;// 修改arr2不会影响arr1System.out.print(arr1的元素为);for(int i 0; i arr1.length; i){System.out.print(arr1[i] );}System.out.println();System.out.print(arr2的元素为);for(int i 0; i arr2.length; i){System.out.print(arr2[i] );}}
} 五数组反转
public class Array08{public static void main(String[] args){int arr[] {1,2,3,4,5};int temp 0;int len arr.length;for(int i 0; i len/2; i){temp arr[i];arr[i] arr[len - 1 - i];arr[len - 1 - i] temp;}for(int i 0; i len; i){System.out.print(arr[i] );}}
} 六数组扩容
要求动态地给数组添加元素效果实现对数组扩容。
1原始数组使用静态分配 int arr[] {1,2,3};
2增加元素4直接放在数组的最后
3用户可以通过如下方法来决定是否继续添加添加成功是否继续y/n public class Array09{public static void main(String[] args){int arr[] {1,2,3};int arrNew[] new int[arr.length 1];for(int i 0; i arr.length; i){arrNew[i] arr[i];}arrNew[arrNew.length - 1] 4;arr arrNew;System.out.print(arr数组的元素为);for(int i 0; i arr.length; i){System.out.print(arr[i] );}}
} // 数组扩容
import java.util.Scanner;
public class Array10{public static void main(String[] args){int arr[] {1,2,3};char choose n;do{System.out.print(请输入需要添加的数字);Scanner myScanner new Scanner(System.in);int newNum myScanner.nextInt();int arrNew[] new int[arr.length 1];for(int i 0; i arr.length; i){arrNew[i] arr[i];}arrNew[arrNew.length - 1] newNum;arr arrNew;System.out.print(添加后的数组元素如下:);for(int i 0; i arr.length; i){System.out.print(arr[i] );}System.out.println();System.out.print(是否继续添加(y/n));choose myScanner.next().charAt(0);}while(choose y);}
} 使用链表可以优化 七数组缩减
要求有一个数组可以将该数组进行缩减提示用户是否继续缩减每次缩减最后那个元素。当只剩下最后一个元素提示不能再缩减。
// 数组缩减
import java.util.Scanner;
public class Array11{public static void main(String[] args){int arr[] {1,2,3,4,5,6,7};System.out.print(当前的数组元素如下:);for(int i 0; i arr.length; i){System.out.print(arr[i] );}System.out.println();System.out.print(是否缩减数组(y/n));Scanner myScanner new Scanner(System.in);char choose myScanner.next().charAt(0);while(choose y){if(arr.length 1){System.out.print(数组元素只剩一个不能再继续缩减);break;}int arrNew[] new int[arr.length - 1];for(int i 0; i arrNew.length; i){arrNew[i] arr[i];}arr arrNew;System.out.print(缩减后的数组元素如下:);for(int i 0; i arr.length; i){System.out.print(arr[i] );}System.out.println();System.out.print(是否继续缩减(y/n));choose myScanner.next().charAt(0);}}
} 二、排序
排序是将一群数据依指定的顺序进行排列的过程。
排序的分类
1、内部排序指将需要处理的所有数据都加载到内部存储器中进行排序包括交换式排序法、选择式排序法和插入式排序法。
2、外部排序数据量过大无法全部加载到内存中需要借助外部存储进行排序包括合并排序法和直接合并排序法。
冒泡排序
基本思想通过对待排序列从后向前从下标较大的元素开始依次比较相邻元素的值若发现逆序则交换使值较大的元素逐渐从前移向后部就像水底下的气泡一样逐渐向上冒。
// 冒泡排序
import java.util.Scanner;
public class BubbleSort{public static void main(String[] args){int arr[] {24,69,80,57,13};int temp;for(int i 0; i arr.length; i){for(int j 0; j arr.length - i - 1; j){if(arr[j] arr[j 1]){temp arr[j];arr[j] arr[j 1];arr[j 1] temp;}}}for(int i 0; i arr.length; i){System.out.print(arr[i] );}}
} 三、查找
常用的查找有两种顺序查找、二分查找
顺序查找
案例有一个数列白眉鹰王、金毛狮王、紫衫龙王、青翼蝠王。猜数游戏从键盘中任意输入一个名称判断数列中是否包含此名称。要求如果找到了就提示找到并给出下标值。
// 顺序查找
import java.util.Scanner;
public class SeqSearch{public static void main(String[] args){String arr[] {白眉鹰王,金毛狮王,紫衫龙王,青翼蝠王};System.out.print(请输入名字);Scanner myScanner new Scanner(System.in);String name myScanner.next();boolean flag true;for(int i 0; i arr.length; i){if(name.equals(arr[i])){System.out.println(已找到该名字下标值为 i);flag false;break;}}if(flag){System.out.println(找不到该名字);}}
} 四、多维数组——二维数组
一二维数组的使用
1、使用方式1——动态初始化
语法 类型 数组名[][] new 类型[大小][大小] int a[][] new int[2][3]; 2、使用方式2——动态初始化
1先声明类型 数组名[][];
2再定义开辟空间数组名[][] new 类型[大小][大小];
3赋值有默认值
3、使用方式3——动态初始化——列数不确定
动态创建下面二维数组 public class TwoDimensionArray02{public static void main(String[] args){int arr[][] new int[3][];for(int i 0; i arr.length; i){arr[i] new int[i 1];for(int j 0; j arr[i].length; j){arr[i][j] i 1;}}for(int i 0; i arr.length; i){for(int j 0; j arr[i].length; j){System.out.print(arr[i][j] );}System.out.println();}}
}4、使用方式4——静态初始化
public class TwoDimensionArray01{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}};for(int i 0; i arr.length; i){for(int j 0; j arr[i].length; j){System.out.print(arr[i][j] );}System.out.println();}}
}二二维数组使用注意事项和细节
1、一维数组的声明方式有int[] x 或 int x[]
2、二维数组的声明方式有int[][] y 或 int[] y[] 或 int y[][]
3、二维数组实际上是由多个一维数组组成的它的各个一维数组的长度可以相同也可以不相同。比如map [][] {{1,2},{3,4,5}};有map[0]是一个含有两个元素的一维数组map[1]是一个含有三个元素的一维数组构成也称为列数不等的二维数组。 文章转载自: http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.lwzpp.cn.gov.cn.lwzpp.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.rksg.cn.gov.cn.rksg.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.pctql.cn.gov.cn.pctql.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.rkdnm.cn.gov.cn.rkdnm.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com