简体   繁体   English

将指向ArrayList的LinkedList的指针转换为String

[英]turning pointer to LinkedList of ArrayList into String

I'm trying to make a linkedList (elements) of custom arrayList (coordinated )that with type Object the Output I am looking for is 我正在尝试创建自定义arrayList(coordinated)的linkedList(元素),该对象的类型为Object我正在寻找的输出是

Arraylist1 coord1 [2,5,1]
Arraylist2 coord2 [7,6,9]
LinkedList List1 [[2,5,1],[7,6,9]]

this is my output 这是我的输出

[ 2 5 1 ] [ 7 6 9 ] [2 5 1] [7 6 9]

assignment1.arrayList@33909752 assignment1.arrayList@33909752

assignment1.arrayList@55f96302 assignment1.arrayList@55f96302

I tried using toString method with no luck can someone please explain to me how to print the output with no pointers to memory. 我尝试使用toString方法没有运气,有人可以向我解释如何在没有指向内存的情况下打印输出。 and do I need a method to access a specific element in a certain position (in the linkList) and get its coordinates? 我是否需要一种方法来访问特定位置(在linkList中)的特定元素并获取其坐标?

here is my code: thanks 这是我的代码:谢谢

public class arrayList {
private Object[] myList;
private int counter = 0;
private int capacity = 100;

public arrayList() {
    myList = new Object[this.capacity];
}

public Object get(int index) {
    if (index < counter) {
        return myList[index];
    } else {
        throw new ArrayIndexOutOfBoundsException();
    }
}

public void add(Object obj) {
    myList[counter++] = obj;
}

public Object remove(int index) {
    if (index < counter) {
        Object obj = myList[index];
        int temp = index;
        myList[index] = null;

        while (temp < counter) {
            myList[temp] = myList[temp + 1];
            myList[temp + 1] = null;
            temp++;
        }

        counter--;
        return obj;
    } else {
        throw new ArrayIndexOutOfBoundsException();
    }
}

public int size() {
    return counter;
}

public void display(Object obj1) {
    System.out.print("[");
    for (int i = 0; i < this.size(); i++) {
        System.out.print(" " + this.get(i) + " ");
    }
    System.out.print("]");
    System.out.println();

}

} }

public class linkedList {

public Cube firstLink;
public Cube next;

linkedList() {
    firstLink = null;
}

public void insertFirstLink(Object e) {

    Cube newLink = new Cube(e);

    newLink.next = firstLink;
    firstLink = newLink;
}

public boolean isEmpty() {
    return (firstLink == null);
}

public Cube removeFirst() {

    Cube linkReference = firstLink;

    if (!isEmpty()) {
        firstLink = firstLink.next;
    } else {
        System.out.println("Empty Linked list!");

    }
    return linkReference;

}

public void display() {

    Cube theLink = firstLink;

    while (theLink != null) {
        theLink.display();
        theLink = theLink.next;
        System.out.println();
    }
}

public Cube find(Object obj) {
    Cube theLink = firstLink;
    if (!isEmpty()) {
        while (theLink.obj != obj) {
            if (theLink.next == null) {
                return null;
            } else {
                theLink = theLink.next;
            }
        }

    } else {
        System.out.println("Empty List!");
    }

    return theLink;
}

public Cube removeLink(Object obj) {
    Cube currentLink = firstLink;
    Cube previousLink = firstLink;

    while (currentLink.obj != obj) {
        if (currentLink.next == null) {
            return null;
        } else {
            previousLink = currentLink;
            currentLink = currentLink.next;
        }
    }

    if (currentLink == firstLink) {
        firstLink = firstLink.next;
    } else {

        previousLink.next = currentLink.next;
    }
    return currentLink;

}

} }

public class Cube {
public Object obj;
public Cube next;

public Cube(Object obj) {
    this.obj = obj;

}

public void display() {
    obj.toString();
    System.out.println(obj);
}

public static void main(String[] args) {

    arrayList coord1 = new arrayList();
    coord1.add(new Integer(2));
    coord1.add(new Integer(5));
    coord1.add(new Integer(1));

    arrayList coord2 = new arrayList();
    coord2.add(new Integer(7));
    coord2.add(new Integer(6));
    coord2.add(new Integer(9));


    coord1.display(coord1);
    coord2.display(coord2);


    linkedList position1 = new linkedList();
    position1.insertFirstLink(coord1);
    position1.insertFirstLink(coord2);

    position1.display();


}

} }

According to the API, https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(java.lang.Object) 根据API, https: //docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println( java.lang.Object)

You put a Object into System.out.println(x) will eventually call String.valueOf(x) which utlimitly call x.toString() . 您将对象放入System.out.println(x)最终将调用String.valueOf(x) ,后者最终将调用x.toString() Put this #1 把这个#1

In linkedList.display() it will call Cube.display() . linkedList.display() ,它将调用Cube.display() Cube.display() will prints its encapsulated object. Cube.display()将打印其封装的对象。 The object turns out to be an arrayList . 该对象原来是arrayList From #1, the method call would become arrayList.toString() . 从#1开始,方法调用将变为arrayList.toString() However, arrayList did not implement toString() . 但是, arrayList没有实现toString() Don't worry, Java got your back. 不用担心,Java支持您。 Since all non-primitive type extends Object, arrayList will has is toString() method inherited from Object. 由于所有非基本类型都扩展了Object,所以arrayList将具有从Object继承的toString()方法。 This is the Object implementation, to print the object id. 这是对象实现,用于打印对象ID。 If you don't like it, you can define your own toString() in arrayList scope. 如果您不喜欢它,则可以在arrayList范围内定义自己的toString()。

Update 1 更新1

You may wonder why the first arraylist could print something meaningful out. 您可能想知道为什么第一个arraylist可以打印出有意义的内容。 It is because you call its arraylist.display() directly, not System.out.println(coord1) 这是因为您直接调用其arraylist.display() ,而不是System.out.println(coord1)

Update 2 更新2

There are some anti pattern in your code. 您的代码中有一些反模式。

  1. Class name should be in camel case , like Cube so as ArrayList, LinkedList. 类名应采用驼峰形式 ,例如Cube,例如ArrayList,LinkedList。
  2. Why arrayList.display() need a argument which also reference to itself but you never use it (ie obj1 is totally ignored)? 为什么arrayList.display()需要一个也引用自身的参数,但您却从未使用过它(即obj1被完全忽略)? coord1.display() is good enough, you don't need coord1.display(coord1) coord1.display()足够好,您不需要coord1.display(coord1)

You need to do following changes: 您需要进行以下更改:

In your arrayList class you need to change methods display(Object obj1) and override toString() method: 在您的arrayList类中,您需要更改方法display(Object obj1)并重写toString()方法:

public void display(Object obj1) {
    System.out.println(obj1);
}

@Override
public String toString() {

    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (int i = 0; i < counter - 1; i++) {
        sb.append(myList[i] + ",");
    }

    sb.append(myList[counter - 1] + "]");
    return sb.toString();
}

In class linkedList change display and override toString() as below: 在类linkedList中,更改显示并覆盖toString(),如下所示:

public void display() {
    System.out.println(this.toString());
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Cube temp = firstLink;
    sb.append("[");
    while (temp != null) {
        sb.append(temp.obj.toString());
        temp = temp.next;
        if (temp != null)
            sb.append(",");
    }
    sb.append("]");

    return sb.toString();
}

} }

Note: It will be printing linked list in reverse order, because your implementation of LinkedList is storing data in reverse order. 注意:它将以相反的顺序打印链表,因为LinkedList的实现是以相反的顺序存储数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM