1 一维数组
(1)数组的定义:
int a = new int[10];
int[] b; b = new int[10];
int[] c ={1,2,3,4,5} ;
(2)数组的引用: int x = a[0];
(3)对象数组:每个元素都需要初始化
1 public class Dog { 2 private String name; 3 private int weight; 4 5 public Dog(String name, int weight) { 6 //super(); 7 this.name = name; 8 this.weight = weight; 9 } 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public int getWeight() { 17 return weight; 18 } 19 public void setWeight(int weight) { 20 this.weight = weight; 21 } 22 23 24 }
1 public class DogTest { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 Dog[] dogs = new Dog[3]; 6 dogs[0] = new Dog("Tom", 11); 7 dogs[1] = new Dog("Kate", 22); 8 dogs[2] = new Dog("Jack", 13); 9 System.out.println(dogs[1].getName()); 10 } 11 12 }
(4)数组的注意事项:
- 数组可以存放同一种数据类型;
- 简单数据类型,可以直接复制;
- 对象数组在定义后,赋值时需要在为每一个对象分配空间(new出每一个对象);
- 数组大小必须事先制定;
- 数组名可以理解为执行数组首地址的引用;
- 数组的下标从0开始;
2 排序
排序(Sorting),是数据处理中很重要且常用的运算,一般数据处理工作25%的时间都在进行排序,是讲一组记录(元素)按照某个域的值得递增或者递减的次序重新排列的过程。排序可分为内部排序和外部排序两种:
- 内部排序:将需要处理的数据全部加载到内部存储器进行排序,包括交换式排序、选择式排序和插入式排序;
- 外部排序:数据量过大时,数据无法全部加载到内存中,需要借助外部存储进行排序,包括合并排序和直接合并排序。
2.1 交换排序
(1)冒泡排序(Bubble Sort):重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
1 /***********************************************************/ 2 /** 3 * 冒泡排序法 4 * @param list 5 * @return 6 */ 7 public static int[] bubbleSort(int[] list){ 8 for (int i = list.length-1; i > 0; i--) { 9 for (int j = 0; j < i; j++) { 10 if(list[j]>list[j+1]){ 11 int temp = list[j]; 12 list[j] = list[j+1]; 13 list[j+1] = temp; 14 } 15 } 16 } 17 return list; 18 } 19 /*************************************************************/
(2)快速排序(Qiuck Sort)
1 package com.sort; 2 /** 3 * 快速排序算法实现的类 4 * @author Cheng 5 * 6 */ 7 public class QuickSort { 8 /** 9 * 10 * @param list 11 * @param left 12 * @param right 13 * @return 14 */ 15 public static int[] sort(int[] list,int left, int right){ 16 int i = left; 17 int j = right; 18 int pivot = list[j]; 19 20 while(i < j){ 21 while(i<j && list[i]<pivot) i++; 22 if(i<j){ 23 list[j] = list[i]; 24 j--; 25 } 26 27 while(i<j && list[j]>pivot) j--; 28 if(i<j){ 29 list[i] = list[j]; 30 i++; 31 } 32 } 33 list[j] = pivot; 34 if(j-1>left){ 35 sort(list, left,j-1); 36 } 37 if(j+1<right){ 38 sort(list,j+1,right); 39 } 40 41 return list; 42 } 43 }
package com.sort; import java.util.Random; public class QuickSortDrive { public static void main(String[] args) { // TODO Auto-generated method stub Random rdm = new Random(); int number = 10000000; int[] list = new int[number]; for (int i = 0; i < list.length; i++) { list[i] = rdm.nextInt(1000); } long t0 = System.currentTimeMillis(); QuickSort.sort(list, 0, list.length-1); long t1 = System.currentTimeMillis(); System.out.println(number+" 个随机数,利用快速排序法排序的耗时为:"+(t1-t0)); } }
2.2 选择排序
(1)简单选择排序法(Selection Sort )
1/**************************************************************/2/**3*选择排序法4*@paramlist5*@return6*/7publicstaticint[]simpleSlectionSort(int[]list){8for(inti=0;i<list.length-1;i++){9intindex=i;10for(intj=i+1;j<list.length;j++){11if(list[index]>list[j]){12index=j;13}14}15if(index!=i){16inttemp=list[i];17list[i]=list[index];18list[index]=temp;19}20}21returnlist;22}23/***********************************************************/
(2)堆排序法(Heap Sort)
(1)直接插入排序(Insertion Sort)
1/***********************************************************/2/**3*插入排序法4*@paramlist5*@return6*/7publicstaticint[]InsertionSort(int[]list){8//如果是空数组或者数值里只有一个元素,则直接将该数组返回9if(list==null||list.length<2){10returnlist;11}else{12//否则开始排序13for(inti=1;i<list.length;i++){14for(intj=i;j>0;j--){15if(list[j]<list[j-1]){16inttemp=list[j];17list[j]=list[j-1];18list[j-1]=temp;19}20}21}2223returnlist;24}25}26/***********************************************************/
(2)希尔排序(Shell Sort)
(3)二叉树排序(Binary-tree Sort)
3 查找
3.1 二分查找(BinarySearch)
1 package com.search; 2 3 /** 4 * 实现二分查找的类 5 * @author Cheng 6 * 7 */ 8 public class BinarySearch { 9 /** 10 * 11 * @param list 参数列表里给出的数组必须是有序数组 12 * @param x 13 * @return 14 */ 15 public static int search(int[] list, int x){ 16 int i = 0; 17 int j = list.length-1; 18 19 while(i<j){ 20 int middleIndex = (i+j)/2; 21 if(x<list[middleIndex]){ 22 j = middleIndex-1; 23 }else if(x>list[middleIndex]){ 24 i = middleIndex+1; 25 }else{ 26 return middleIndex; 27 } 28 } 29 30 return -1; 31 } 32 }
4 多维数组
二维数组: int[][] aList = new iint[2][3];
5 二进制
对于有符号的数而言——
- 二进制的第一位是符号位,0表示正数,1表示负数
- 正数的原码、反码和补码都一样
- 而负数的反码:原码的符号位不变,其他位取反
- 负数的补码:等于其反码+1
- 0的反码和补码都是0
- java所有的数都是有符号的
- 计算机运算中以补码进行运算
(1)原码
(2)反码
(3)补码
6 位运算 4个
对二进制数补码每个位上的数值进行操作
& 按位与 (二进制屏蔽其他位:int x = (n&8)/8)
| 按位或
^ 按位异或
~ 按位取反(包括符号位)
7 移位运算符 3个
针对二进制数补码
>>算术右移:低位溢出,符号位不变,并用符号位补溢出的高位
<<算术左移:符号位不变,低位补0
>>>逻辑右移:低位溢出,高位补0