J2SE _ Array

Source: Internet
Author: User

Array Overview

Declaration of a one-dimensional array

creation of Array objects

element is an array of reference data types

array Initialization (1)

array Initialization (2)

default initialization of array elements

references to array elements

 Public classTestargs { Public Static voidMain (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); }     DoubleD1 = double.parsedouble (args[0]); DoubleD2 = double.parsedouble (args[2]); Doubled = 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 classtestarray{ Public Static voidMain (string[] args) {int[] A =New int[Args.length];  for(inti=0; i<args.length; i++) {A[i]=Integer.parseint (Args[i]); } System.out.print ("Before sorting:");        Print (a); System.out.print ("\ n Sort after:"); //Bubble_sort (a);Selectionsort (a);    Print (a); }    //Select Sort    Private Static voidSelectionsort (int[] a) {         for(inti=0;i<a.length;i++){             for(intj=i+1; j<a.length;j++){                if(a[i]>A[j]) {                    intTemp=0; Temp=A[i]; A[i]=A[j]; A[J]=temp; }            }        }    }    //Select Sort Improvements    Private Static voidSelectionsort (int[] a) {        intk, temp;  for(inti=0;i<a.length;i++) {k=i;  for(intj=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; }        }    }    //Bubble Sort    Private Static voidBubble_sort (int[] a) {         for(inti=0;i<a.length-1;i++){             for(intj=0; j<a.length-i-1;j++){                if(A[j] > a[j+1]){                    inttemp = 0; Temp=A[j]; A[J]= A[j+1]; A[j+1] =temp; }            }        }    }    //Print one-dimensional array    Private Static voidPrintint[] a) {         for(inti=0; i<a.length;i++) {System.out.print (A[i]+" "); }    }}
Sort.java

Classroom Exercises

 Public classTestdatesort { Public Static voidMain (string[] args) {date[] days=NewDate[5]; days[0] =NewDate (2006, 5, 4); days[1] =NewDate (2006, 7, 4); days[2] =NewDate (2008, 5, 4); days[3] =NewDate (2004, 5, 9); days[4] =NewDate (2004, 5, 4); Date D=NewDate (2006, 7, 4); String Str=string.valueof (d); //str = d.tostring ();Bubblesort (days);  for(inti=0; i<days.length; i++) {System.out.println (days[i]);    } System.out.println (BinarySearch (days, D)); }          Public Staticdate[] Bubblesort (date[] a) {intLen =a.length;  for(inti = len-1;i>=1;i--){             for(intj = 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; }            }        }        returnA; }         Public Static intBinarySearch (date[] days, Date d) {if(days.length==0)return-1; intstartpos = 0; intEndpos = days.length-1; intm = (startpos + endpos)/2;  while(Startpos <=endpos) {          if(D.compare (days[m]) = = 0)returnm; if(D.compare (days[m]) > 0) {startpos= m + 1; }          if(D.compare (days[m]) < 0) {Endpos= M-1; } m= (startpos + endpos)/2; }        return-1; }}classDate {intYear , month, day; Date (intYintMintd) { year= y; month = m; Day =D; }     Public intCompare (date date) {returnYear > Date.year? 1: Year< date.year? -1: Month> Date.month? 1: Month< Date.month? -1: Day> Date.day? 1: Day< Date.day? -1:0; }     PublicString toString () {return"Year:Month:Day--" + year + "-" + Month + "-" +Day ; }}
Testdatesort.java

Add 3 back 1

 Public classCount3quit { Public Static voidMain (string[] args) {Boolean[] arr =New Boolean[500];  for(inti=0; i<arr.length; i++) {Arr[i]=true; }                intLeftcount =arr.length; intCountnum = 0; intindex = 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(inti=0; i<arr.length; i++) {            if(Arr[i] = =true) {System.out.println (i); }        }    }}
Count3quit.java

J2SE _ Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.