数组9.27
一.数组概念
数组:数组是一组有序数据的集合。数组中的排列是有一定的规律的下标代表数据在数组中的序号(
数组下标从0开始)
eg int a[]={1,2,3,5,6,8,9}; 其中数组元素有7个即为a[0]-a[6];
.1.基本数据类型的数组
(1).数组动态初始化
声明开辟数组(数组实例化):
数据类型 数组名称 [] = new 数据类型 [长度];
其中数据类型 即声明数组,new即开辟数组,
int arr [] = new int [7];
(2).数组静态初始化(静态数组缺陷就是长度固定)
数据类型 数组名称[] = new 数据类型 [] {};
int arr[] = new int [] {1,2,3,5,6};
2.数组与方法调用(代码表示)
public class ArrayDemo{
public static void main(String[] args) {
int a [] = new int [] {1,2,3,5,6,9,7,8,4};
printArray(a);//指定一个输出数组的方法
}
}
打印数组方法:
public static void printArray(int temp []){
for(int i=0;i < temp.length ; i++){
System.out.println( temp [i]);
}
System.out.println();
}
上述代码将数组通过printArray方法输出输出结果为:
1 2 3 5 6 9 7 8 4
3.数组排序(目前先说两种方法后续会补充)
1.方法(java.util.Arrays.sort(数组))
public class ShuZuPaiXu{
public static void main(String[] args) {
int a [] = new int [] {1,2,3,5,12,9,7,8,4};
java.util.Arrays.sort(a);
printArray(a);
}
public static void printArray(int temp []){
for(int i=0;i < temp.length ; i++){
System.out.println( temp [i]);
}
System.out.println();
}
}
2.冒泡法
public class MaoPao{
public static void main(String[] args){ //主方法中的代码尽可能少,简单一点
int a[]= new int [] {9,8,7,4,6,3,2,1,0,5};
sort(a);
printArray(a);
}
public static void sort(int arr[]) {//表示一个排序的方法
for(int i=0;i < arr.length - 1; i++){
for(int j = 0; j<arr.length - i -1; j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] =arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void printArray(int temp []){//输出数组的方法
for(int i=0;i < temp.length ; i++){
System.out.println( temp [i] + "");
}
System.out.println();
}
}
输出结果
0 1 2 3 4 5 6 7 8 9
4.数组拷贝
方法:System.arraycopy(原数组名称,开始位置,接收数组名称,开始位置,长度);
public class ShuZuKaoBei{
public static void main(String[] args)
{
int a [] = new int [] {1,2,3,4,5,6,7,8,9,};
int b [] = new int [] {11,22,33,44,55,66,77,88,99};
System.arraycopy(b,4,a,1,3);// 从b数组的第四个元素开始,长度为三,拷贝到a数组中,a数组的第一个元素为起点
printArray(a);
}
public static void printArray(int temp []){
for(int i=0;i < temp.length ; i++){
System.out.println( temp [i]);
}
System.out.println();
}
}
输出结果
1 55 66 77 5 6 7 8 9
5.数组转置
一维数组转置
public class ZhuanZhi {
public static void main(String[] args)
{
int a[] = new int [] {9,8,7,6,5,4,3,2,1,0};//一维数组的转置
reverse (a);//调用转置的方法
printArray(a);//定义一个专门输出a的方法
}
public static void reverse(int b[]) {
int head = 0;
int tail = b.length - 1;
for(int i =0 ; i<b.length/2 ; i++){
int temp = b[head];
b[head] = b[tail];
b[tail] = temp;
head++;
tail--;
}
}
public static void printArray( int temp []){
for(int j = 0 ; j < temp.length; j++){
System.out.println( temp [j]+".");
}
System.out.println();
}
}
输出结果
0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
二维数组转置(此处的二维数组必须为行列相等)
public class ZhuanZhi {
public static void main(String[] args)
{
int a[][] = new int [][] {{1,2,3},{4,5,6},{7,8,9}};
reverse(a);
printArray(a);
}
public static void reverse( int b[][]){ //定义的转置方法
for(int i= 0 ; i<b.length ; i++){
for(int j= i ; j <b[i].length ;j++){//b[i]也是一个数组
if(i != j){
int temp = b[i][j];
b[i][j] = b[j][i];
b[j][i] = temp;
}
}
}
}
public static void printArray(int temp[][]){
for(int i= 0 ; i<temp.length ; i++){
for(int j= 0 ; j <temp[i].length ;j++){
System.out.println(temp[i][j]+ ",");
}
System.out.println();
}
}
}
输出结果
1, 4, 7, 2, 5, 8, 3, 6, 9
二.对象数组类型(保存内容比基本数据类型数组要广)
对象数组动态态初始化:
类名称 数组名称 [] = new 类名称 [长度] ;eg:Person per [] = new Person [3] ;
class Person{
private String name ;
private int age ;
public Person(){}
public Person(String name,int age){
setName(name);
setAge(age);
}
public void setName(String n){
name = n;
}
public void setAge(int a){
age = a;
}
public String getName() {
return name;
}
public int getAge(){
return age;
}
public String getInfo(){
return "姓名 :"+ name +",年龄 : "+ age;
}
}
public class ShuZuFangFaDiaoYong{
public static void main(String[] args)
{
Person per[] = new Person[3];//对象数组动态初始化
per[0] = new Person("张三",18);
per[1] = new Person("李四",21);
per[2] = new Person("刘某",20);
for(int i=0;i<per.length;i++){
System.out.println(per[i].getInfo());
}
}
}
运行结果
姓名 :张三,年龄 : 18
姓名 :李四,年龄 : 21
姓名 :刘某,年龄 : 20
对象数组静态初始化:
类名称 数组名称 [] = new 类名称 [] {实例化};
class Person{
private String name ;
private int age ;
public Person(){}
public Person(String name,int age){
setName(name);
setAge(age);
}
public void setName(String n){
name = n;
}
public void setAge(int a){
age = a;
}
public String getName() {
return name;
}
public int getAge(){
return age;
}
public String getInfo(){
return "姓名 :"+ name +",年龄 : "+ age;
}
}
public class ShuZuFangFaDiaoYong{
public static void main(String[] args)
{
Person per[] = new Person[]{
new Person("张三",18),
new Person("李四",21),
new Person("刘某",20)};//对象数组静态初始化
for(int i=0;i<per.length;i++){
System.out.println(per[i].getInfo());
}
}
}
输出结果
姓名 :张三,年龄 : 18
姓名 :李四,年龄 : 21
姓名 :刘某,年龄 : 20
总结
遇到的错误提示:
ArrayIndexOutOfBoundsException
数组越界(就是数组下标超出数组的长度)