A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© nuddlesW 中级黑马   /  2014-11-15 11:02  /  877 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

java学习笔记-1113-数组
数组:同一种类型数据的集合,其实数组就是一个容器。
格式: 元素类型[]数组名 = new 元素类型[元素个数]
           元素类型[]数组名 = new 元素类型[]{元素1,元素2……}
           元素类型[]数组名 ={}file:///C:/Users/Administrator/AppData/Local/YNote/Data/fanzhouyou193756@163.com/5471f1f6cef34028878e8ab0d4555900/clipboard.png

数组要注意的问题:
ArrayIndexOutOfBoundsException:操作数组时切记不能访问不存在的角标。如int[] arr= new int[3],角标最大的为arr[2];
NullPointerException:空指针异常,引用了没有任何指向的数组角标。
数组长度:array.length
数组遍历:for 循环。
System.out.print(数组名);该方法打印的是数组的引用地址,哈希算法

1 /*
2 要求:冒泡排序
3 思路:两两比较,最大数冒泡到最后一个位置。
4 */
5  import java.util.*;
6  class TestArray {
7    public static void sort(int[] a) {//定义方法
8                       for(int i = a.length;i>0;i--){
9                                for(int j = 0;j< i-1;j++) {//编程过程一定要注意下标变化
10                                                 if(a[j]<a[j+1]) {
11                                                          inttemp =a[j];
12                                                          a[j]= a[j+1];
13                                                          a[j+1]=temp;
14                                                          }
15                                                 }
16                                       }                                             
17                     }
18           /*
19           要求:在指写数组中查找某一个元素。
20           思路:折半查找
21           */
22           
23           public static int search(int[] arr,intkey) {
24                     int min = 0,max=arr.length-1;
25                     while(min<=max){
26                                       int mid=(min+max)>>1;
27                                       if(arr[mid]>key){
28                                                 max= min-1;
29                                       }elseif(arr[mid]<key){
30                                                 min=min+1;
31                                       }else{
32                                                 intindex =mid;
33                                                 returnindex;
34                                                 }
35                              }
36                              return -1;
37                     }
38           public static void main(String[] args){//主方法
39                              int[] arr={1,2,3,4,7,8};
40                              /*Arrays.sort(arr);
41                              for(int i=0;i<arr.length;i++) {
42                                       System.out.print(arr);
43                                       }*/
44                                       System.out.print(search(arr,7));
45                     }
46   }

1 /*
2 要求:优化进制转换
3 思路:采用位移和查表的方式
4 */
5 class transform{
6 public static void toType(int x,int y,intoffset) {
7  char[] c={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
8  char[] chr =new char[32];
9  int index =31; //定义指针
10  while(x != 0){
11   int temp =x&y;
12   chr[index--]=c[temp];//倒着存
13     x= x>>offset;
14   }
15   for(int i =index+1;i<32;i++){//从指针处开始遍历打印
16  System.out.print(chr);
17   }
18  }
19  public static void main(String[]args) {
20   toType(60,15,4);
21   toType(6,1,1);
22   int [][] arr =new int [3][];
23   }
24

java可以直接调用的排序方法 Arrays.sort();

二维数组:
举例说明:
int[][]  arr = new int [x][] 行数必须先定义。
arr.length 二维数组长度 即行数,
arr[0].length  第一个一维数组的长度。

数组定义的一些注意点:[][] 顺序无关  如int[]x,[]y   int []x ,int [][] y
java内存结构
new 的实体都在堆里,堆内存中的实体都有默认初始化值,堆里的东西,如果没有引用后,会自动回收。



评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 赞一个!加油

查看全部评分

1 个回复

正序浏览
好 不错不错
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马