java:手写MyLinkedList所有方法,增删改查
package arrays.myArray;
public class MyLinkedList {
private int size = 0;
private Node1 head = null;
// 添加
public void add(Object obj) {
add(size, obj);
}
// 修改
public void add(int index, Object obj) {
if (null == head) {
head = new Node1(obj, null);
} else {
if (0 == index) {
head = new Node1(obj, head);
} else {
Node1 temp = head;
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
temp.next = new Node1(obj, temp.next);
}
}
size++;
}
// 移除
public void remove(int index) {
if (0 == index) {
head = head.next;
} else {
Node1 temp = head;
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
}
size--;
}
// 查询
public Object get(int index) {
Node1 temp = head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp.obj;
}
// 总长度
public int size() {
return size;
}
}
class Node1 {
Object obj;
Node1 next;
public Node1(Object obj, Node1 next) {
this.obj = obj;
this.next = next;
}
}
相关文档:
再次从网上查询,搜到了RXTXcomm.jar包比较好,是封装了comm.jar的方法。
安装:
1.copy rxtxSerial.dll to [JDK-directory]\jre\bin\rxtxSerial.dll
2.copy RXTXcomm.jar to [JDK-directory]\jre\lib\ext\RXTXcomm.jar
&nbs ......
(一)线程同步
实现生产者消费者问题来说明线程问题,举例如下所示:
/**
* 生产者消费者问题
*/
public class ProducerConsumer {
/**
* 主方法
*/
public static void main(String[] args) {
ProductBox pb = new ProductBox ......
中国公历算法不是太难,关键是星期值的确定。这里给出了简单算法:
public static int dayOfWeek(int y, int m, int d) {
int w = 1; // 公历一年一月一日是星期一,所以起始值为星期日
y = (y-1)%400 + 1; //&n ......
package arrays.compara;
/**
*
* @author Happy 二分查找法
*/
public class BinarySearch {
public static void main(String[] args) {
int[] arrInt = { 2, 34, 32, 24, 23, 34, 12, 3, 4, 2 };
int index = bSearch(29, arrInt, 0, arrInt.length);
& ......
package arrays.myArray;
public class BinaryTree {
private Node root;
// 添加数据
public void add(int data) {
// 递归调用
if (null == root)
root = new Node(data, null, null);
else
addTree(root, data);
......