Java数据结构和算法(二)——数组
来源:互联网 发布:手机上淘宝密码怎么改 编辑:程序博客网 时间:2024/05/16 14:57
转自http://blog.csdn.net/iaiti/article/details/39493403
数组的用处是什么呢?——当你需要将30个数进行大小排列的时候,用数组这样的数据结构存储是个很好的选择,当你是一个班的班主任的时候,每次要记录那些学生的缺勤次数的时候,数组也是很有用。数组可以进行插入,删除,查找等。
1)创建和内存分配
Java中有两种数据类型,基本类型和对象类型,也有人称为引用类型,Java中把数组当成对象,创建数组时使用new操作符。
- int array[] = new int[10];
初始化:
- public class UseArray {
- public static void main(String[] args) {
- int array[] = new int[10];
- System.out.println(array[2]);
- UseArray a[] = new UseArray[12];
- System.out.println(a[1]);
- int array2[] ={1,2,3,4,5,5,6};
- }
- }
2)数组封装后的使用
- public class UseArray {
- private int[] array;
- private int number = 0;
- public UseArray(int max){
- array = new int[max];
- }
- public void insert(int value){
- array[number] = value;
- number++;
- }
- public int find(int value){
- for (int i= 0; i < number; i++) {
- if(array[i]==value)
- return i;
- }
- return number;
- }
- public boolean delete(int value){
- int index = find(value);
- if(index != number){
- for (int i = index; i < number-1; i++) {
- array[i] = array[i+1];
- }
- number--;
- return true;
- }
- return false;
- }
- public void display(){
- for (int i = 0; i < number; i++) {
- System.out.printf(array[i]+" ");
- }
- }
- public static void main(String[] args) {
- UseArray ua = new UseArray(5);
- ua.insert(1);
- ua.insert(2);
- ua.insert(6);
- ua.insert(7);
- ua.insert(3);
- ua.display();
- if(ua.find(5) != ua.number){
- System.out.println("find,the number index is "+ua.find(5));
- }else{
- System.out.println("not found!");
- }
- if(ua.delete(5)!=true){
- System.out.println("can not delete!");
- }
- ua.display();
- }
- }
将整个数组封装,用number代替数组的个数,插入数据的时候也不必理会往哪个下标插,当然,也可以自定义一个具体下标的方法。
方法比较简单就不介绍了,但是存在的一个缺点在delete那里,其实只是从删除元素开始的左移而已,所以,虽然number减少了,但是最后一个元素并没有删除掉,只是display输出展示的时候隐藏了而已,但是,下次插入元素的时候新元素便会取代掉最后一个元素的位置。
3)查找优化——二分查找
- public int find(int value){
- int start = 0;
- int end = number-1;
- while(end>=start){
- int index =(end + start)/2;
- if(array[index]==value){
- return index;
- }else if(array[index] >value){
- end = index-1;
- }else {
- start = index+1;
- }
- }
- return number;
- }
排序的话倒是看—— Java数据结构和算法(三)——简单排序。
存储对象的话原理一样。
4)大O表示法
设N为数据总数,加入插入一个数据时间为K。
那么线性查找总时间T=K*N/2,因为查找的话大概为比较数目的一半。
二分查找的话T=k*log2(N)。
大O表示法,O可以看成是order of,大约是的意思,k/2也是常数,所以可以看成是O(N)。
阅读全文
0 0
- Java数据结构和算法(二)——数组
- Java数据结构和算法(二)——数组
- Java数据结构和算法(二)——数组
- Java数据结构和算法-数组
- Java 数据结构和算法 数组
- JAVA数据结构和算法(二)—线性查找
- 《java数据结构与算法》——数组
- 数据结构与算法之—数组(二)
- 【Java】Java数据结构和算法(二)——栈和队列
- Java数据结构和算法系列2--数组
- Java数据结构和算法-数组(续)
- Java数据结构和算法:数组、单链表、双链表
- 数据结构回顾版-java数据结构-有序数组和查找算法
- 数据结构和算法------数组
- Java数据结构和算法——数组、单向链表、双向链表
- Java数据结构和算法——数组、单向链表、双向链表
- 二、数据结构与算法--数组
- Java数据结构与算法之数组(二)
- QT中Http的阻塞型Get和Post
- github使用问题
- 表关系多对多,1对多,1对1 映射到数据库时出现的一些异常(重要)
- HDFS 常用shell命令
- 那些设计出来就不希望别人看懂的C代码——IOCCC国际模糊C代码大赛
- Java数据结构和算法(二)——数组
- 92. Reverse Linked List II
- Lecture 4: Decomposition and abstraction through functions; introduction to recursion
- Redis 字符串(String)
- 重新开始写字的一点感想
- Lecture 6: Bisection methods, Newton/Raphson, introduction to lists
- C++ const 的使用
- Hadoop分布式集群搭建
- 数组、链表、堆栈和队列