数组
- 数组是相同类型数据的有序集合
- 数组描述的是相同类型的若干的数据,按照一定的先后次序排列组合而成
- 其中,每个数据称作每个数组元素,每个数组元素可以通过一个下标来访问
数组的声明与创建
声明数组变量,才能在程序中使用数组
数组里的元素是按照索引访问的,索引从零开始。
声明数组变量
/*
声明数组
dataType[] arrayRefVar; // 首选的方法
或
dataType arrayRefVar[]; // 效果相同,但不是首选方法
*/
/*
创建数组并赋值
arrayRefVar = new dataType[arraySize];
arrayRefVar [] = value ;
*/
public class Demo {
public static void main(String[] args) {
int[] int1 ;//声明int数组,名为int1
int1 = new int [10] ;//创建有10个空间数组
int1[0] = 10 ;//给数组第一个数赋值10
System.out.println ("int1 [0]=" + int1 [0]);//输出
}
}
输出结果为
给数组元素循环赋值
public class Demo {
public static void main(String[] args) {
int[] int1 ;//声明int数组,名为int1
int1 = new int [10] ;//创建有10个空间数组
for (int i = 0 ;i <int1.length;i ++){ /*定义整数i,arrays.length获取数组长度*/
int1 [i] = i ; //将i循环赋值给数组对应的元素
System.out.println ( int1 [i]);//输出各个元素
}
}
}
初始化以及内存分析
内存分析
- 堆
存放New的对象和数组
所有线程共享,不会存放别的对象引用
- 栈
存放基本变量类型(会包含那个基本类型的具体数值)
引用对象的变量(会保存这个引用在堆里面的具体地址)
- 方法区
可以被所有线程共享
包含了所有class和static变量
三种初始化
- 静态初始化
int [] a = {1,2,3} ; //创建 + 赋值 (静态初始化)
Man [] mans = {new Man(1,1),new Man(2,2)} ;
- 动态初始化
int [] a = new int[2] ; //包含默认初始化 (动态初始化,默认值为0)
a [0] = 1 ;
a [1] = 2 ;
- 数组的默认初始化
数组是引用类型,它的元素相当于实例变量,一旦定义将被默认初始化,默认值为0.
数组的使用
基础使用
- 遍历数组中所有元素
public class Demo {
public static void main(String[] args) {
int[] a = {1,2,3,4,5} ;
for (int b = 0 ;b<a.length;b++){ //遍历数组元素
System.out.println(a[b]);
}
}
}
- 所有元素的和
public class Demo {
public static void main(String[] args) {
int d = 0 ;
int[] a = {1,2,3,4,5} ;
for (int c = 0 ;c <a.length; c++){
d = d + a[c] ;
}
System.out.println("所有元素和 =" + d);
}
}
- 数组中元素最大值
public class Demo {
public static void main(String[] args) {
int[] a = {1,2,3,4,5} ;
int Max = a[0] ;
for (int e = 0 ; e<a.length ; e++){
if (Max < a[e]){
Max = a[e] ;
}
}
System.out.println("Max =" + Max);
}
}
进阶使用
- For – Each循环
JDK1.5以上出现的功能,相当于jAVA自己自带循环
public class Demo {
public static void main(String[] args) {
int[] a = {1,2,3,4,5} ;
for (int b : a ){
//for - each遍历数组
System.out.println(b);
}
}
}
- 输出数组的元素
public class Demo {
public static void main(String[] args) {
int[] a = {1,2,3,4,5};
Text(a) ;
}
public static void Text (int[] a){
for (int b = 0 ;b<a.length;b++){ //遍历数组元素
System.out.print(a[b] + "\n");
}
}
}
- 反转数组
public class Demo {
public static void main(String[] args) {
int[] a = {1,2,3,4,5};
int[] II = TText(a) ;
Text(II);
}
public static void Text (int[] a){
for (int b = 0 ;b<a.length;b++){ //遍历数组元素
System.out.print(a[b] + "\n");
}
}
public static int[] TText (int[] a) {
int[] G = new int[a.length] ;
for (int Y = 0,U = a.length - 1;Y<a.length;Y++,U--){
G[U] = a[Y] ;
}
return G;
}
}
二维数组
每个元素都是一维数组的数组
int[] [] a = new int [2] [5] ;
int [] [] a = {{1,2,3,4,5},{6,7,8,9,0}} ;
遍历二维数组
public class Demo {
public static void main(String[] args) {
int[] [] a = {{1,2},{3,4},{5,6}};
for (int i = 0;i<a.length;i++){ //{1,2}{3,4}{5,6}
for (int t =0;t<a[i].length;t++){ //1,2,3,4,5,6
System.out.println(a[i][t]);
}
}
}
}
泛型
泛型的本质就是”参数化类型”。一提到参数,最熟悉的就是定义方法的时候需要形参,调用方法的时候,需要传递实参。那”参数化类型”就是将原来具体的类型参数化.
目的是为了避免强制格式转换带来的ClassCastException,类型转化异常。
不使用泛型
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
try {
List list = new ArrayList();
list.add("String");
list.add(123);
for ( int i = 0;i < list.size();i++){
System.out.println((String) list.get(i)); //(String)强制转化
}
}catch (ClassCastException classCastException){
System.out.println(classCastException);
}
}
}
运行这段代码会发现出现了强制转换带来的ClassCastException
转化异常。
就需要使用泛型来规范化
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>(); //使用泛型
list.add("String");
list.add(123);
for (int i = 0; i < list.size(); i++) {
System.out.println((String) list.get(i));
}
}
}