数组与冒泡排序

  1 package shuzu;
  2 
  3 import java.util.Arrays;
  4 
  5 public class ssss {
  6 
  7     public static void main(String[] args) {
  8         
  9         //一维数组
 10         
 11         int myarry [] = new int [5];
 12         
 13         //索引从0开始
 14         
 15         myarry[0] = 100;
 16       
 17         myarry[2] = 200;
 18          
 19         for(int i =0; i<myarry.length;i++)
 20         {
 21             System.out.println("myarry=" + myarry [i]);
 22         }
 23         
 24           //第二种方式,初始化数组,遍历
 25          
 26          int my[]  =new int [] {0,1,2,3,4};                     
 27 
 28             for(int i =0; i<my.length;i++)
 29             {
 30                 System.out.println("my=" + my [i]);
 31             }
 32         
 33             //第三种方式
 34             
 35             
 36             char [] csz = new char [5];
 37             
 38             csz [0] =    'f';
 39             
 40             double [] dsz = {1,2,3};
 41             
 42             long lsz [] = new long [ ] {1,3,5,4};
 43             
 44             long l =lsz [2];
 45             
 46             //操作数组
 47             Arrays.sort(lsz );    //排序
 48             
 49             //遍历  while 循环
 50             
 51             int i =0;
 52             
 53             while (i<lsz.length)
 54             {
 55                System.out.println("lsz [" + i + "] = "+ lsz[i]);
 56                i++;
 57                 Arrays.sort(lsz );    //排序
 58             }
 59             //复制数组
 60             
 61             long [] lsz2 =lsz;
 62             long [] lsz3 =Arrays.copyOf(lsz,2);
 63             long [] lsz4 =Arrays.copyOfRange(lsz,1,3);
 64             
 65             //查询数组,返回索引值,如果么有找到返回负数,可以判断是否包含某元素
 66         
 67             System.out.println("8的索引位置=" +Arrays.binarySearch(lsz,8));
 68             
 69             //填充
 70             Arrays.fill(lsz2,2);
 71             Arrays.fill(lsz3,1, 3, 8);
 72             
 73             
 74             //foreach语句
 75             
 76             for (double d :dsz)
 77             {
 78                 System.out.println("d=" + d);
 79             }
 80             
 81             //二维数组
 82             
 83             int [] [] ewsz = new int [2] [3];
 84             
 85             ewsz [0] = new int [] {1,2,3};
 86             ewsz [1] = new int [] {4,5,6};
 87             
 88             System.out.println("ewsz=" + ewsz [1] [2]);
 89             
 90             System.out.println("ewsz=" + ewsz .length);
 91             
 92             for (int [] ie : ewsz)                                                    //foreach循环
 93             { 
 94                 for (int f : ie)
 95                 {
 96                     System.out.print( f +" ");
 97                 }
 98                 System.out.println();
 99             }
100             
101             for (int m = 0; m < ewsz.length;m++)                     //for循环
102             {
103                 for (int n =0; n< ewsz [m].length; n++)
104                 {
105                     System.out.print("" +ewsz [m] [n]);
106                 }
107                 System.out.println();
108             }
109             
110             long [] [] lesz = new long [2] [];
111             
112             lesz [0] = new long [] {1,5};
113             lesz [1] = new long [] {2,3,4,5,};
114             
115             for (int m = 0; m < lesz.length;m++)                     //for循环
116             {
117                 for (int n =0; n< lesz [m].length; n++)
118                 {
119                     System.out.print("" +lesz [m] [n]);
120                 }
121                 System.out.println();
122             }
123             
124             
125             
126         
127 
128     }
129 
130 }
 1 package maopao;
 2 
 3 public class xxx {
 4 
 5     public static void main(String[] args) {
 6         
 7         //冒泡排序
 8         
 9         int [] sz = {23,12,56,97,19,30,7,21};
10         
11         for (int i : sz)
12         {
13             System.out.print(i +",");
14         }
15         System.out.println();
16         for (int j =1;j<sz.length;j++)
17         {
18             for (int i =0; i<sz.length - j; i++)
19             {
20                 //可以改变序列方式
21                 if (sz [i] > sz [i+1])
22                 {
23                     int t = sz [i+1];
24                     sz [i+1] =sz [i];
25                     sz [i] =t;
26                 }
27             }
28             }
29         
30         
31         for (int i : sz)
32         {
33             System.out.print(i +",");
34         }
35         System.out.println();
36     
37     }
38 
39 }

 

posted @ 2015-10-09 18:15  雨鹭  阅读(229)  评论(0编辑  收藏  举报