码迷,mamicode.com
首页 > 编程语言 > 详细

J2SE之_数组

时间:2015-09-02 01:49:22      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

数组概述

技术分享

一维数组的声明

技术分享

数组对象的创建

技术分享

 元素为引用数据类型的数组

技术分享

 数组初始化(1)

技术分享

 数组初始化(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);
    }
}
TestArgs.java

 

技术分享
public class TestArray
{
    public static void main(String[] args){
        int[] a = new int[args.length];
        for(int i=0; i<args.length; i++){
            a[i] = Integer.parseInt(args[i]);
        }
        System.out.print("未排序前:");
        print(a);
        System.out.print("\n排序后为:");
        //bubble_sort(a);
        selectionSort(a);
        print(a);
    }

    //选择排序
    private static void 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=0;
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
    }

    //选择排序改进
    private static void selectionSort(int[] a){
        int k, temp;
        for(int i=0;i<a.length;i++){
            k = i;
            for(int j=k+1; j<a.length;j++){
                if(a[j]>a[k]){
                    k = j;
                }
            }
            if(k != i){
                temp = a[i];
                a[i] = a[k];
                a[k] = temp;
            }
        }
    }


    //冒泡排序
    private static void bubble_sort(int[] a){
        for(int i=0;i<a.length-1;i++){
            for(int j=0; j<a.length-i-1;j++){
                if(a[j] > a[j+1]){
                    int temp = 0;
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }
    //打印一维数组
    private static void print(int[] a){
        for(int i=0; i<a.length;i++){
            System.out.print(a[i]+" ");
        }
    }
}
Sort.java

 

 课堂练习

技术分享

技术分享
public class TestDateSort {
    public static void main(String[] args) {
        Date[] days = new Date[5];
        days[0] = new Date(2006, 5, 4);
        days[1] = new Date(2006, 7, 4);
        days[2] = new Date(2008, 5, 4);
        days[3] = new Date(2004, 5, 9);
        days[4] = new Date(2004, 5, 4);
        
        Date d = new Date(2006, 7, 4);
        String str = String.valueOf(d);
        //str = d.toString();
        bubbleSort(days);
        
        for(int i=0; i<days.length; i++) {
            System.out.println(days[i]);
        }
        
        System.out.println(binarySearch(days, d));
    }
    
     public static Date[] bubbleSort(Date[] a){
        int len = a.length;
        for(int i = len-1;i>=1;i--){
            for(int j = 0;j<=i-1;j++){
                if(a[j].compare(a[j+1]) > 0){
                    Date temp = a[j]; 
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        return a;
    }
    
    public static int binarySearch(Date[] days, Date d) {
        if (days.length==0) return -1;
    
        int startPos = 0; 
        int endPos = days.length-1;
        int m = (startPos + endPos) / 2;
        while(startPos <= endPos){
          if(d.compare(days[m]) == 0) return m;
          if(d.compare(days[m]) > 0) {
              startPos = m + 1;
          }
          if(d.compare(days[m]) < 0) {
              endPos = m -1;
          }
          m = (startPos + endPos) / 2;
        }
        return -1;
    }
}

class Date {
  int year, month, day;
  
  Date(int y, int m, int d) {
    year = y; month = m; day = d;
  }
  
  public int compare(Date date) {
    return year > date.year ? 1
           : year < date.year ? -1
           : month > date.month ? 1
           : month < date.month ? -1
           : day > date.day ? 1
           : day < date.day ? -1 : 0;
  }
  
  public String toString() {
      return "Year:Month:Day -- " + year + "-" + month + "-" + day;
  }
}
TestDateSort.java

 

加3退1

技术分享
public class Count3Quit {
    public static void main(String[] args) {
        boolean[] arr = new boolean[500];
        for(int i=0; i<arr.length; i++) {
            arr[i] = true;
        }
        
        int leftCount = arr.length;
        int countNum = 0;
        int index = 0;
        
        while(leftCount > 1) {
            if(arr[index] == true) {
                countNum ++;
                if(countNum == 3) {
                    countNum = 0;
                    arr[index] = false;
                    leftCount --;
                }
            }
            
            index ++;
            
            if(index == arr.length) {
                index = 0;
            }
        }
        
        for(int i=0; i<arr.length; i++) {
            if(arr[i] == true) {
                System.out.println(i);
            }
        }
    }
}
Count3Quit.java

 

J2SE之_数组

标签:

原文地址:http://www.cnblogs.com/gimin/p/4777432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!