数组

数组:可以看成是多个相同的数据类型的集合.对这些数据进行统一处理.C 和C++都可以放在栈上.java不能它是引用类型.
数组的声明:type var[] 或 type[] var; 
  示例: int a[] 或 int[] a;
        Stirng str[] 或 String[] str;
        Person p[] 或 Person[] p;
注意: java语言声明数组时不能事先定义其长度,例如:int a[5]//非法.
      java中使用关键字new来创建数组对象,格式为: 
      数组名 = new 数组元素的类型[数组元素的个数]
  示例: 1.int [] s ;
        s = new int[6]; //分析内存的分配过程. 
        2.Date[] date;
          date = new Date[3];  //分析在内存的分配过程.


数组的初始化:数组定义与为数组分配空间和赋值的操作分开进行. 
  //动态初始化
public class DomeInit {
public static void main(String args[]){
int[] a ;
a = new int[3];
a[0] = 3;
a[1] = 5;
a[2] = 8;
Date[] dates;
dates = new Date[3];
dates[0] = new Date(1990,2,1);
dates[1] = new Date(1923,3,2);
dates[2] = new Date(1939,3,9);
}
}


class Date {
int year,month,day;
Date(int y,int m ,int d){
this.year = y;
this.month = m;
this.day = d;
}
}


静态初始化: 在定义数组元素时分配空间并且赋值.
  例: int a ={3,9,8};


数组元素默认初始化:数组是引用数据类型,它的元素相当于类的成员变量,
                   因此分配 空间后也按照成员变量的规则被隐式的初始化.
    int===0 , float====0.0 ,对象=======null;
    
数组元素的引用 :
  1.通过数组的一个下标值可以引用这个对象.下标值是从0开始.
  2.每个数组都有一个属性length指明它的长度.
  3.数组args是接收命令参数.
  示例:
  public static void main(String[] args) {
       if(args.length<3){
      System.out.println("please write n1  算术运算符(加减乘除) n2");
      System.exit(0);
       }
       int  n1 = Integer.parseInt(args[0]);
       int  n2 = Integer.parseInt(args[2]);
       if(args[1].equals("+")){
      System.out.println(n1+n2);
       }else if(args.equals("-")){
      System.out.println(n1-n2);
       }else if(args.equals("*")){
      System.out.println("n1*n2");
       }else if(args.equals("/")){
      System.out.println("n1/n2");
       }
}
}


数组常规排序算法:
 冒泡排序: 
  public static void main(String[] args) {
int[] a;
a = new int[10];
     Random r1 = new Random();
     for(int i=0;i<a.length;i++){
    a[i] =  r1.nextInt(100); 
     }
     
     for(int i=0;i<a.length;i++){
    System.out.print(" " + a[i]);
     }
     System.out.println("");
     
     for(int i=0;i<a.length;i++){
       for(int j=i+1;j<a.length;j++){
      if(a[i]>a[j]){
      int temp;
      temp = a[i];
      a[i] = a[j];
      a[j] = temp;
      }
       }
      }
     
     for(int i=0;i<a.length;i++){
    System.out.print(" "+a[i]);
     }
    
}
选择排序:核心代码
   int k,temp;
     for(int i=0;i<a.length;i++){
    k=i;
       for(int j=k+1;j<a.length;j++){
      if(a[k]>a[j]){
     k=j;  
      }
       }
       if(k!=i){
      temp = a[i];
  a[i] = a[k];
  a[k] = temp;
       }
      }
对一个日期对象的数组进行排序:
  public class DomeSort {


public static void main(String[] args) {
    Date1 date11 = new Date1(1923,12,5);
    Date1 date12 = new Date1(1913,2,2);
    Date1 date13 = new Date1(1923,1,2);
    Date1 date14 = new Date1(1923,12,2);
    Date1 date15 = new Date1(1953,8,2);
    Date1 date16 = new Date1(1983,6,2);
    Date1 [] date2 = {date11,date12,date13,date14,date15,date16};
    
    for(int i=0;i<date2.length;i++){
    for(int j=i;j<date2.length;j++){
    Date1 datetemp ;
    if(date2[i].compare(date2[j])==0){
    datetemp = date2[i];
    date2[i] = date2[j];
    date2[j] = datetemp;
    }
    }
    }
    
    for(int i=0;i<date2.length;i++){
    System.out.println(date2[i]);
    }
}
}


class Date1 {
int year,moth,day;
Date1(int year ,int moth,int day){
this.year = year;
this.moth = moth;
this.day = day;
}

public int compare(Date1 date){
return this.year<date.year ? -1 : this.moth<date.moth ? -1: this.day <date.day ? -1 :0; 
}

@Override
public String toString() {
return "yerar: " + year + " month " +moth + " day " + day;
}
}


满3去1
public class DomeTest {
public static void main(String args[]){
boolean[] arr ;
arr = new boolean[500];
for(int i=0;i<500;i++){
arr[i] = true;
}
 
int countnum = arr.length;
int step =0;
int index = 0;
while(countnum > 1){
if(arr[index] = true){
step++;
if(step == 3){
arr[index] = false;
countnum--;
step=0;
}
}
   index ++;
   
if(index == arr.length){
index = 0;
}
}

for(int i=0;i<arr.length;i++){
if(arr[i]==true){
System.out.println(i);
}
}
}
}


二维数组:
 二维数组可以看成以数组为元素的数组: 例如:
    int a[][]= {{1,2},{1,23,3,}};
 java中多维数组的声明和初始化应按从高维到低维的顺序。
 int a[][] = new int[3][];
 int a[0][] = new int[2];
 int a[1][] = new int[3];
 int b[][] = new int[][3];//非法。
 
静态初始化: 
 int a[3][2] ={{1,2},{2,3},{3,4}};//非法。
 
动态初始化: 
 int a[][] = new int[3][5];
 
二维数组的遍历:
 public class DometwoArray {


public static void main(String[] args) {
     int a[][] = {{1,3},{2,3,2},{3,5,2,1}};
     for(int i=0;i<a.length;i++){
    for(int j=0;j<a[i].length;j++){
    System.out.print(a[i][j]);
    }
      System.out.println("");  
     }
}
}


数组的拷贝:数组的区域往往是连续的。所以拷贝的最高效的方法是统一把内存拷贝一份。
使用:java.lang.System类的静态方法:
可以用于数组SRC 从第srcPos项元素开始的length个元素拷贝到目标数组从
                  第destPos项开始的length个位置。
如果源数据数目超过目标数组边界会抛出: IndexOutOfBoundsException异常。
public class DomeArrayCopy {


public static void main(String[] args) {
      int[] arr = {1,3,2,1,3};
      int[] arrbak = new int[6];
      System.arraycopy(arr, 0, arrbak, 0, arr.length);
      arr[1]  = 4;
      for(int i=0;i<arrbak.length;i++){
    System.out.print(arrbak[i]);  
      }
      System.out.println(" ");
      
      int[][] arr1 = {{1,2},{2,3},{3,4}};
      int[][] arr1bak = new int[3][];
      System.arraycopy(arr1, 0, arr1bak, 0, arr1.length);
      arr1[0][1] = 3;
      for(int i=0;i<arr1bak.length;i++){
     for(int j=0;j<arr1bak[i].length;j++){
     System.out.print(arr1bak[i][j] + " ");
     }
     System.out.println(" ");
      }
}
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值