`JAVA视频学习笔记-马士兵`
JAVA视频学习笔记(五)
数组概述(20200608)
- 数组概念及特点:
数组:是一种容器,可以同时存放多个数据值;
特点:数组是一种引用类型;数组中多个数据,类型必须统一;数组的长度在程序运行期间不可改变。
和C、C++不一样,他们的数组可以分配到栈上,但是JAVA不行。
一维数组内存分析(20200609)
- 一维数组:。
2. 数组对象的创建
3. 一维数组的内存分析
数组元素的创建和使用(20200609)
- 数组初始化-动态初始化:
- 数组初始化-静态初始化:
3. 数组初始化-默认初始化:
4. 数组元素的引用
数组的练习(20200610)
- 练习:。
public class TestEnum {
public static void main(String[] args) {
int[] a = {2, 4, 6, 7, 3, 5, 1, 9, 8};
for(int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
/*for(int i=0; i<args.length; i++) {
System.out.println(args[i]);
}
直接运行不会有输出,直接跟命令行参数,直接输出命令行参数值,待使用。
命令行参数
ipconfig -all*/
}
}}
输出结果:
2.命令行参数
public class TestArgs {
public static void main(String[] args) {
/*
for(int i=0; i<args.length; i++) {
System.out.println(args[i]);
}
System.out.println(
"Usage: java Test \"n1\" \"op\" \"n2\"");
*/
if(args.length<3){
System.out.println(
"Usage: java Test \"n1\" \"op\" \"n2\"");
System.exit(-1);
}
double d1 = Double.parseDouble(args[0]);
double d2 = Double.parseDouble(args[2]);
double d = 0;
if(args[1].equals("+")) d = d1+d2;
else if(args[1].equals("-")) d = d1-d2;
else if(args[1].equals("x")) d = d1*d2;
else if(args[1].equals("/")) d = d1/d2;
else{
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(d);
}
}
3.排序
public class TestSort {
public static void main(String args[]) {
int[] a ={1,7,3,9,2,5,8,4,6};
//bubbleSort(a);
//selectionSort(a);
insertSort(a);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
public static int[] bubbleSort(int[] a){
int len = a.length;
for(int i = len-1;i>=1;i--){
for(int j = 0;j<=i-1;j++){
if(a[j]>a[j+1]){
int temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
public static int[] selectionSort(int[] a) {
for(int i=0; i<a.length; i++) {
for(int j = i+1; j < a.length; j++) {
if(a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
public static int[] insertSort(int[] a) {
for(int i=1; i<a.length; i++) {
for(int j=i; j>0; j--) {
if(a[j] < a[j-1]) {
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
} else {
break;
}
}
}
return a;
}
}
二维数组(20200611)
- 二维数组:数组的数组。
数组举例:
public class TestArrayCopy {
public static void main(String args[]) {
int[][] intArray = {{1,2},{1,2,3},{3,4}};
int[][] intArrayBak = new int[3][];
System.arraycopy
(intArray,0,intArrayBak,0,intArray.length);
intArrayBak[2][1] = 100;
for(int i = 0;i<intArray.length;i++){
for(int j =0;j<intArray[i].length;j++){
System.out.print(intArray[i][j]+" ");
}
System.out.println();
}
}
}
数组的拷贝(20200612)
- 拷贝:挨个依次拷贝效率比较低。
public class TestArrayCopy {
public static void main(String args[]) {
String[] s =
{"Mircosoft","IBM","Sun","Oracle","Apple"};
String[] sBak = new String[6];
System.arraycopy(s,0,sBak,0,s.length);
for(int i=0;i<sBak.length;i++){
System.out.print(sBak[i]+" ");
}
System.out.println();
int[][] intArray = {{1,2},{1,2,3},{3,4}};
int[][] intArrayBak = new int[3][];
System.arraycopy
(intArray,0,intArrayBak,0,intArray.length);
intArrayBak[2][1] = 100;
for(int i = 0;i<intArray.length;i++){
for(int j =0;j<intArray[i].length;j++){
System.out.print(intArray[i][j]+" ");
}
System.out.println();
}
}
}
数组总结(20200613)
- 数组总结:
(1)数组的内存布局
所有人都明白这里定义了一个数组,其包含了5个int型的数据。我们可以用a[0],a[1]等来访问数组里面的每一个元素,那么这些元素的名字就是a[0],a[1]…吗?看下面的示意图:
如上图所示,当我们定义一个数组a时,编译器根据指定的元素个数和元素的类型分配确定大小(元素类型大小*元素个数)的一块内存,并把这块内存的名字命名为a。名字a一旦与这块内存匹配就不能被改变。a[0],a[1]等为a的元素,但并非元素的名字。数组的每一个元素都是没有名字的。
(2)常见算法
java数组常见算法。