jase总结--10(数组)
来源:互联网 发布:python控制树莓派gpio 编辑:程序博客网 时间:2024/06/07 02:02
数组
任意相同类型的数据
按照先后顺序依次存放,连续空间
每一个数据称为一个元素
下标/索引
Length-1
Java数组的特点
l 数组的长度是固定的
初始化时指定大小后不再改变
l 数组数据类型要统一
数组可以声明为任何数据类型,包括任何基本数据类型和引用数据类型。
数组的某个元素被传递给一个方法并被该方法修改,当被调用方法执行完毕时,这个元素中含有修改过的数值。(void也是如此)
声明并初始化之后,数组的各个位置都被赋予了默认值。
声明数组的两种方式
String[] str;//类型 [] 标识符(推荐)
Int str[];//类型 标识符 []
初始化数组
A
int a[]={1,2,3,4};
B.
int b[4]={1,2,3,4};
C.
int c[];c={1,2,3,4};
D.
int d[];d=new int[]{1,2,3,4};
C.
String [ ] s=new String [10];
D.
double[ ] d [ ]=new double [4][ ];
A.
float []f[] = new float[6][6];
B.
float f[][] = new float[][];
C.
float [6][]f = new float[6][6];
D.
float [][]f = new float[6][];
String [] strArray=new String[20];
Student stu=new Student[100];
数组元素的索引可以是整型常量、整型变量、整型表达式等任意数据类型。
赋值
strArray[0]=”aaa”;
遍历数组并赋值
for(){}
静态初始化
声明的同时赋值
动态初始化
声明之后赋值
不利用第三值进行两数互换
int a = 5;
int b = 8;
a = a+b;
b = a-b;
a = a-b;
System.out.println(a+"---"+b);
GirlFriend[] girlFriends = new GirlFriend[100];
girlFriends[0] = new GirlFriend("如花");
girlFriends[1] = new GirlFriend("芙蓉");
girlFriends[2] = new GirlFriend("玉凤");
class GirlFriend {
private String name;public GirlFriend(String name) {
this.name = name;
}
}
声明二维数组
String[][] arrays =null;
arrays =new String[5][3];
int[][]arrays02 = { { 1, 2 }, { 1, 2, 3 }, { 1, 2, 3, 4 } };
for (int i = 0; i < arrays02.length;i++) {
System.out.println(arrays02[i].length);
}
String[][] s =null;
s =new String[3][];
s[0] =new String[2];
s[1] =new String[3];
s[2] =new String[2];
//int intB[3][2] = {{1,2},{2,3},{4,5}};//非法
被final的数组不能再填写内容。
final int[]nums =new int[10];
//The final local variable nums cannot be assigned. It must be blank and not using a compound assignment
//nums = new int[20];
声明二维数组
String[][] arrays =null;
arrays =new String[5][3];
int[][]arrays02 = { { 1, 2 }, { 1, 2, 3 }, { 1, 2, 3, 4 } };
for (int i = 0; i < arrays02.length;i++) {
System.out.println(arrays02[i].length);
}
String[][] s =null;
s =new String[3][];
s[0] =new String[2];
s[1] =new String[3];
s[2] =new String[2];
//int intB[3][2] = {{1,2},{2,3},{4,5}};//非法
被final的数组不能再填写内容。
final int[]nums =new int[10];
//The final local variable nums cannot be assigned. It must be blank and not using a compound assignment
//nums = new int[20];
常用方法
l System
* 数组的拷贝
* System.arraycopy(src, srcPos, dest, destPos, length);
* src:源数组
* srcPos:拷贝的起始位置(源数组)
* dest:目标数组
* destPos:目标数组的起始位置
* length:拷贝的多少(长度)
*
l 打印
int[] n1 = { 1, 7, 2, 3, 9, 4, 0, 5, 6 };
Arrays.toString(n1);
System.out.println(Arrays.toString(n1));
l 计算HASH值
int[] n1 = { 1, 7, 2, 3, 9, 4, 0, 5, 6 };
Arrays.hashCode(n1);
l 比较
int[] n1 = { 1, 2, 3, 4, 5 };
int[] n2 = { 1, 2, 3, 4, 5 };
System.out.println(n1 == n2);
System.out.println(Arrays.equals(n1, n2));
l 覆盖
int[] n1 = { 1, 2, 3, 4, 5, 6 };
Arrays.fill(n1, 100);
l 排序
int[] n1 = { 1, 7, 2, 3, 9, 4, 0, 5, 6 };
Arrays.sort(n1);
l 查找(二分查找法的前提是有序)(二分查找法)
//查找输入的字符显示其位置
int[]n1 = { 1, 7, 2, 3, 9, 4, 0, 5, 6 };
Arrays.sort(n1);
int index = Arrays.binarySearch(n1, 8);
System.out.println(Arrays.toString(n1));
System.out.println(index);
多维数组的拷贝
//一般拷贝主要拷贝一维数组,因为程序员容易把控
int[][]n1 = { { 1, 2, 3 }, { 4, 5, 7, 8 }, { 1, 2, 3 } };
int[][]n2 = new int[2][3];
System.arraycopy(n1, 0,n2, 0, 2);
for (int i = 0; i < n2.length;i++) {
for (int j = 0; j < n2[i].length;j++) {
System.out.print(n2[i][j] + "\t");
}
System.out.println();
}
}
- jase总结--10(数组)
- JASE总结--09(1.抽象类2.接口3.Debug4.垃圾回收机制5.内部类6.方法内部类7.静态内部类)
- JASE总结--07(构造器、this、overload、static、静态代码块、package、import、静态引入)
- jase--接口学习
- 指针和数组(总结)
- Java基础知识总结(数组)
- js数组方法(总结)
- js总结(数组API)
- JAVA总结(数据结构--数组)
- Array 数组去重 总结10方法(7)
- 数组总结
- 数组总结
- 数组总结
- 数组总结
- 数组总结
- 数组总结
- 数组总结
- *数组总结*
- ajax实现异步任务
- light oj 1078 - Integer Divisibility
- AudioFlinger,AudioPolicyService and Audio track分析
- 单链表
- 瑞萨单片机教程(2013) -R8C系列 输入输出端口配置
- jase总结--10(数组)
- 在使用oracle中遇到的一些问题
- SDP Profile-level-id解析
- view 构造函数
- 用循环求解
- Java中对日期时间处理
- poj-1985-Cow Marathon【树的直径】
- C++, ID、指针、handle (void *)的区别
- hdu 1878 无向图的欧拉回路