一、字符串常见的操作
1、结合我在python中对于字符串常见处理总结出下面三个应该能算是字符串造作的TOP 3
1 //字符串查找子串,存在返回index,不存在返回-1 等于python的find: 2 String string = "abcdefg"; 3 string.indexOf("cd"); 4 5 //字符串的替换: 6 string = string.replace("old","new"); 7 8 //字符串的拆分: 9 stringlist = string.split(","); 10 for(String each:stringlist){ 11 System.out.println(each); 12 }
2、这里涉及一个常见的增强循环,有点类似于python中对可迭代对象的迭代
1 for(数据类型 item:迭代对象){ 2 do_something(); 3 }
二、自实现数据
1、关于基本数组的概念,这里就不在赘述了,也没啥说的,这里主要想自己实现一个动态数组,结合python的list感觉就是一个实现动态数组的绝佳例子,而且也方便做队列和栈等。
可以实现append、extend、remove、iterator、切片、len等基本功能。如果想实现FIFO或者FILO类的数据结构(队列或栈)则继承之后实现push和pop等操作就好了。
1 /* 2 * 目的:为了实现类似python的list的功能而定义这个数据结构 3 * 作者:陈然 4 * 版本:v0.1 5 * 声明:高度python爱好者 6 */ 7 8 /*引入包文件*/ 9 import java.io.*; 10 import java.lang.*; 11 import java.util.Iterator;//为了实现增强循环引入迭代器 12 13 /*定义基础类myList的数据结构*/ 14 class List implements Iterable<Object>{ 15 /*我的仿Python的Java的ist数据结构*/ 16 /*定义成员属性*/ 17 protected Object[] array;//可以传入任意对象的list的存储空间 18 protected int size;//数组的元素数量 19 20 /*定义构造方法*/ 21 List(){} 22 List(int size,Object[] object){ 23 this.size = size; 24 this.array = new Object[object.length]; 25 System.arraycopy(object, 0, this.array, 0, size); 26 } 27 /*定义成员方法*/ 28 public boolean clear() { 29 //清空列表 30 try { 31 this.size = 0; 32 this.array = new Object[0]; 33 return true; 34 } 35 catch(Exception reason){ 36 return false; 37 } 38 } 39 40 public int getSize() { 41 //返回列表长度 42 return this.size; 43 } 44 45 public List getPices(int start,int end) throws Exception { 46 int length = 0; 47 boolean flag = false; 48 if(start >= 0 && end >=start) { 49 length = end-start; 50 } 51 else if(start < 0 && end <= start) { 52 length = start - end; 53 flag = true; 54 } 55 else { 56 throw new Exception(""); 57 } 58 Object[] helper = new Object[length]; 59 int fence = 0; 60 if(!flag) { 61 for(int i=start;i<end;i++) { 62 helper[fence] = this.array[i]; 63 fence++; 64 } 65 }else { 66 for(int i=0;i<length;i++) { 67 helper[fence] = this.array[this.array.length-1-i]; 68 fence++; 69 } 70 } 71 return new List(helper.length,helper); 72 } 73 74 public boolean append(Object object) throws Exception{ 75 try { 76 if(this.size == 0) { 77 try { 78 this.array = new Object[1]; 79 this.array[0] = object; 80 this.size ++; 81 return true; 82 } 83 catch(Exception reason) { 84 return false; 85 } 86 } 87 else { 88 Object[] helper = new Object[this.size+1]; 89 System.arraycopy(this.array, 0, helper, 0, this.array.length); 90 helper[this.size] = object; 91 this.array = new Object[helper.length]; 92 System.arraycopy(helper, 0, this.array, 0, helper.length); 93 this.size++; 94 return true; 95 } 96 } 97 catch(Exception reason) { 98 return false; 99 } 100 } 101 102 public boolean extend(Object[] exarray) throws Exception{ 103 try { 104 if(this.size == 0) { 105 this.array = new Object[exarray.length]; 106 System.arraycopy(exarray, 0, this.array, 0, exarray.length); 107 this.size = this.array.length; 108 return true; 109 } 110 else { 111 Object[] helper = new Object[this.size+exarray.length]; 112 System.arraycopy(this.array, 0, helper, 0, this.size); 113 System.arraycopy(exarray, 0, helper, this.size, exarray.length); 114 this.array = new Object[helper.length]; 115 System.arraycopy(helper, 0, this.array, 0, helper.length); 116 this.size += exarray.length; 117 return true; 118 } 119 }catch(Exception reason) { 120 return false; 121 } 122 } 123 124 public boolean removeByIndex(int index) throws Exception { 125 if(index >= this.size) { 126 return false; 127 } 128 else { 129 try { 130 Object[] helper = new Object[this.size-1]; 131 System.arraycopy(this.array, 0, helper, 0, index); 132 System.arraycopy(this.array, index+1, helper, index, helper.length-index); 133 this.array = new Object[helper.length]; 134 System.arraycopy(helper, 0, this.array, 0, helper.length); 135 this.size --; 136 return true; 137 }catch(Exception reason) { 138 return false; 139 } 140 } 141 } 142 143 public boolean removeByObject(Object object) throws Exception { 144 for(int i=0;i<this.size;i++) { 145 if(this.array[i] == object){ 146 return this.removeByIndex(i); 147 } 148 } 149 return false; 150 } 151 152 @Override 153 public Iterator<Object> iterator() { 154 //重载迭代器 155 return new Iterator<Object>() { 156 int cursor = 0;//索引下标 157 158 @Override 159 public boolean hasNext() { 160 return cursor < size; 161 } 162 163 @Override 164 public Object next() { 165 return (Object)array[cursor++]; 166 } 167 @Override 168 public void remove() {} 169 }; 170 } 171 172 }
知识点1:
1 /*关于数组的拷贝*/ 2 System.arraycopy(sarray,startindex,darray,startindex,copysize);
知识点2:
1 /*异常处理*/ 2 //对于函数定义时候抛出异常 3 public void function_name throws Exception(){} 4 5 //代码块 6 try{ 7 //... 8 }catch(Exception reason){ 9 //... 10 } 11 12 //抛出异常 13 throw new Exception("xxxxxx");