您的位置:首页 > 其它

模拟HashSet

2017-01-12 18:02 253 查看
package chain;

/**
* 单链表 节点(Map中的Entry<K,V>)
*
* @author sd
*
*/
public class Node_Single {
public String key;// 节点的值

public Node_Single next;// 指向下一个的指针

public Node_Single(String key) {// 初始化head
this.key = key;
this.next = null;
}

public Node_Single(String key, Node_Single next) {
this.key = key;
this.next = next;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public Node_Single getNext() {
return next;
}

public void setNext(Node_Single next) {
this.next = next;
}

@Override
public String toString() {
return "Node_Single [key=" + key + ", next=" + next + "]";
}

}
package chain;

import java.io.IOException;

import net.sf.json.JSONArray;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
* 单链表(单个数据组的单链表)
*
* @author sd
*
*/
public class SingleList {
transient static Node_Single[] table = new Node_Single[1];
static int bucketIndex = 0;// 索引
public static ObjectMapper mapper = new ObjectMapper();

/**
* 添加一个元素
*
* @param node
*/
public void addTolist(String key) {
Node_Single e = table[bucketIndex];
table[bucketIndex] = new Node_Single(key, e);
}

public static void main(String[] args) throws JsonGenerationException,
JsonMappingException, IOException {
SingleList sin = new SingleList();
sin.addTolist("1");
sin.addTolist("2");
System.out.println(table[0]);
System.out.println(JSONArray.fromObject(table));
System.out.println(mapper.writeValueAsString(table));
}

}
结果
Node_Single [key=2, next=Node_Single [key=1, next=null]]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/workspace/Test1/WebContent/WEB-INF/lib/slf4j-log4j12-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/workspace/Test1/WebContent/WEB-INF/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
[{"key":"2","next":{"key":"1","next":null}}]
[{"key":"2","next":{"key":"1","next":null}}]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: