java數組的基本使用

來源:互聯網
上載者:User

標籤:java

public class Demo3 {


/**
* @param args
*/
public static void main(String[] args) {// 數組的基本使用
System.out.println("===數組的基本使用===");
int score[]=null;//數組是參考型別,預設值是null
score=new int[3];
for(int x=0;x<3;x++){
System.out.println("score["+x+"]="+score[x]);
}
System.out.println("擷取數組的長度:"+score.length);
System.out.println("===數組的靜態初始化===");
int i[]={1,2,3,4,5,6};
for(int j=0;j<i.length;j++){
System.out.print("i["+j+"]="+i[j]+",");
}
System.out.println("===判斷數組最大值和最小值===");
int k[]={1,2,3,4,5};
int min=0;
int max=0;
min=max=k[0];
for(int i1=0;i1<k.length;i1++){
if(min<k[i1]){
min=k[i1];
}
if(max>k[i1]){
max=k[i1];
}
}
System.out.println("max="+max);
System.out.println("min="+min);
System.out.println("===冒泡排序法的使用===");
int f[]={5,3,6,2,7,1,8};
for(int i2=0;i2<f.length;i2++){
for(int j=0;j<f.length;j++){
if(f[i2]<f[j]){
int temp=f[i2];
f[i2]=f[j];
f[j]=temp;
}
}
}
for(int i3=0;i3<f.length;i3++){
System.out.print(f[i3]+",");
}
System.out.println("===二維數組的基本用法===");
int g[][]=new int[4][3];
g[0][1]=30;
g[1][0]=31;
g[2][2]=32;
g[3][1]=33;
g[1][1]=30;
for(int p=0;p<g.length;p++){
for(int j=0;j<g[p].length;j++){
System.out.print(g[p][j]+"\t");
}
System.out.println();
}
System.out.println("===方法的使用:拆半發===");
getMsg();
}
public static void getMsg(){
int start=0,end,middle;
int a[]={2,9,3,8,5,7,-3};
int N=a.length;
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
if(a[j]<a[i]){
int t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
Scanner scanner=new Scanner(System.in);
System.out.println("判斷一個數是不是在數組中");
int number=scanner.nextInt();
int count=0;
end=N;
middle=(start+end)/2;
while(number!=a[middle]){
if(number>a[middle]){
start=middle;
}else if(number<middle){
end=middle;
}
middle=(start+end)/2;
count++;
if(count>N/2)
break;
}
if(count>N/2)
System.out.printf("%d不在數組中",number);
else
System.out.printf("%d在數組中",number);
}


}

著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

java數組的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.