Java 如何将对象添加到链接列表中?
我一直在从事一个项目,其中我必须实现一个java类,该类实现了双链表的使用。我已经用我所有的方法完成了LinkedList类。我只是不确定如何将节点对象添加到列表中。这是我到目前为止的代码,底部是test。任何帮助都将不胜感激。谢谢Java 如何将对象添加到链接列表中?,java,linked-list,doubly-linked-list,Java,Linked List,Doubly Linked List,我一直在从事一个项目,其中我必须实现一个java类,该类实现了双链表的使用。我已经用我所有的方法完成了LinkedList类。我只是不确定如何将节点对象添加到列表中。这是我到目前为止的代码,底部是test。任何帮助都将不胜感激。谢谢 public class LinkedList { private Node first; private Node current; private Node last; private int currentIndex;
public class LinkedList {
private Node first;
private Node current;
private Node last;
private int currentIndex;
private int numElements;
public LinkedList() {
this.first = null;
this.last = null;
this.numElements = 0;
this.current = null;
this.currentIndex = -1;
}
private class Node {
Node next;
Node previous;
Object data;
}
public boolean hasNext() {
return (current != null && current.next != null);
}
public Object next() {
if (!this.hasNext()) {
throw new IllegalStateException("No next");
}
current = current.next;
return current.data;
}
public boolean hasPrevious() {
return (current != null && current.previous != null);
}
public Object previous() {
if (!this.hasPrevious()) {
throw new IllegalStateException("No previous");
}
current = current.previous;
return current.data;
}
int nextIndex() {
int index = numElements;
if (hasNext()) {
index = this.currentIndex + 1;
}
System.out.println(index + "The current index is " + current);
return index;
}
int previousIndex() {
int index = -1;
if (hasPrevious()) {
index = this.currentIndex - 1;
}
System.out.println(index + "The current index is " + current);
return index;
}
public void set(Object o) {
if (this.current == null) {
throw new IllegalStateException("No node found, cannot set.");
}
current.data = o;
}
public int size() {
return numElements;
}
public void add(Object o) {
Node newNode = new Node();
newNode.data = o;
if (first == null) {
first = newNode;
last = newNode;
newNode.next = null;
} else if (first != null) {
if (current == null) {
newNode.previous = null;
newNode.next = first;
first.previous = newNode;
first = newNode;
} else if (current == last) {
newNode.previous = current;
newNode.next = null;
current.next = newNode;
last = newNode;
} else {
newNode.previous = current;
newNode.next = current.next;
current.next.previous = newNode;
current.next = newNode;
}
}
current = newNode;
numElements++;
currentIndex++;
}
public void remove() {
if (current != null) {
if (current == first && current == last) {
first = null;
last = null;
} else if (current == last) {
current.previous = null;
last = current.previous;
} else if (current == last) {
current.previous.next = null;
last = current.previous;
} else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
current = current.next;
numElements--;
}
}
}
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name;
int index;
LinkedList<Object> listOne = new LinkedList<Object>();
listOne.add(object o);
}
}
公共类链接列表{
私有节点优先;
专用节点电流;
最后是私有节点;
私有int-currentIndex;
私人住宅;
公共链接列表(){
this.first=null;
this.last=null;
这个数值等于0;
this.current=null;
this.currentIndex=-1;
}
私有类节点{
节点下一步;
节点前向;
对象数据;
}
公共布尔hasNext(){
返回(当前!=null&¤t.next!=null);
}
公共对象下一个(){
如果(!this.hasNext()){
抛出新的非法状态异常(“无下一个”);
}
当前=当前。下一步;
返回当前数据;
}
公共布尔值hasPrevious(){
返回(当前!=null&¤t.previous!=null);
}
公共对象上一个(){
如果(!this.hasPrevious()){
抛出新的非法状态异常(“无以前的”);
}
当前=当前。先前;
返回当前数据;
}
int nextIndex(){
整数指数=整数;
if(hasNext()){
索引=此.currentIndex+1;
}
System.out.println(index+“当前索引为”+当前);
收益指数;
}
int previousIndex(){
int指数=-1;
if(hasPrevious()){
index=this.currentIndex-1;
}
System.out.println(index+“当前索引为”+当前);
收益指数;
}
公共无效集(对象o){
if(this.current==null){
抛出新的IllegalStateException(“未找到节点,无法设置”);
}
当前数据=o;
}
公共整数大小(){
回礼;
}
公共无效添加(对象o){
Node newNode=新节点();
newNode.data=o;
if(first==null){
第一个=新节点;
last=newNode;
newNode.next=null;
}else if(first!=null){
如果(当前==null){
newNode.previous=null;
newNode.next=first;
first.previous=newNode;
第一个=新节点;
}else if(当前==上次){
newNode.previous=当前;
newNode.next=null;
current.next=newNode;
last=newNode;
}否则{
newNode.previous=当前;
newNode.next=current.next;
current.next.previous=newNode;
current.next=newNode;
}
}
当前=新节点;
numElements++;
currentIndex++;
}
公共空间删除(){
如果(当前!=null){
如果(当前==第一个和当前==最后一个){
第一个=空;
last=null;
}else if(当前==上次){
current.previous=null;
last=当前的.previous;
}else if(当前==上次){
current.previous.next=null;
last=当前的.previous;
}否则{
current.previous.next=current.next;
current.next.previous=current.previous;
}
当前=当前。下一步;
婚礼--;
}
}
}
导入java.util.Scanner;
公共类LinkedListTest{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
字符串名;
整数指数;
LinkedList listOne=新建LinkedList();
添加(对象o);
}
}
想想编号列表,看看元素之间的关系
假设我有清单:
C的前一个新节点是NewNode。因此,insert函数需要知道前一个节点或下一个节点,然后调整关系。如果您的代码经过编译,我会感到惊讶,因为您的类实际上不是泛型的。只需将其初始化为
LinkedList listOne=new LinkedList()代码>(无尖括号)
至于实际添加元素,您只需要添加一些对象的实例
;一切都可以(假设您的内部代码工作正常)。在结尾处尝试以下方法:
Object objectToAdd = "Strings are Objects";
listOne.add(objectToAdd);
objectToAdd = new File("C:\\foo.bar"); // Or use any other Objects!
listOne.add(objectToAdd);
您的LinkedList
没有泛型类型,因此无法将其声明为
LinkedList<Object> listOne = new LinkedList<Object>();
现在要添加元素,只需使用add
方法
listOne.add("something");
listOne.add(1);//int will be autoboxed to Integer objects
另外,如果您想从键盘添加数据,您可以使用
String line="";
do{
System.out.println("type what you want to add to list:");
line = keyboard.nextLine();
listOne.add(line);
}while(!line.equals("exit"));
线路
LinkedList listOne=新建LinkedList()代码>
除非将类声明更改为,否则不会编译
类链接列表
或者你也可以直接写
LinkedList listOne=新建LinkedList()代码>
之后,您应该能够将对象添加到列表中。但是,您需要创建一个对象来添加到它,listOne.add(objecto)代码>不行——至少你必须编写listOne.add(newobject())
。(你的代码没有实例化一个对象,没有你已经调用过的对象,o
,而且,对象o
在Java中没有任何意义,不会编译。正如人们提到的那样,你的列表不是泛型的。但是当他们建议你去掉参数时,你也可以添加
或
链接到您的链表实现,并保留您的
String line="";
do{
System.out.println("type what you want to add to list:");
line = keyboard.nextLine();
listOne.add(line);
}while(!line.equals("exit"));
public class LinkedList<E>
public class LinkedListTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name;
int index;
LinkedList listOne = new LinkedList();
//Initially we should be empty so we are positioned
// at both the beginning and end of the list
assert listOne.size() == 0 :"List should be empty";
assert listOne.hasPrevious()==false: "Should be at the beginning of the list";
assert listOne.hasNext()==false : "Should be at the end of the list";
Object firstNode = "I am the first node";
listOne.add(firstNode); //we've added something
//I left this commented out since you don't have a current() method.
// assert firstNode == listOne.current() : "Our current item should be what we just added";
assert listOne.hasPrevious()==false : "Should not have moved forward in our list yet";
assert listOne.hasNext()==true : "should have an item after our current";
assert listOne.size() == 1 : "Should only have one item in the list";
Object secondNode = "I am the second node";
listOne.add(secondNode);
assert listOne.size() == 2 : "Should only have two items in the list";
assert firstNode == listOne.next() : "1st call to next should return the 1st node";
assert listOne.hasPrevious()==true : "We should be positioned after the 1st node";
assert listOne.hasNext()==true : "We should be positioned before the 2nd node";
}
}
public class MyLinkedListTest {
public static final void main(String[] args) {
MyLinkedList list = new MyLinkedList();
System.out.println("Number of items in the list: " + list.size());
String item1 = "foo";
String item2 = "bar";
list.add(item1);
list.add(item2);
System.out.println("Number of items in the list: " + list.size());
// and so on...
}
}