本文介绍了我们如何创建双链表节点到以逗号分隔的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个项目,它要求我在双链接列表接口中生成一个toString()。我们可以使用StringBuilder和追加方法,但我想通过返回它们而不使用任何内置的java方法来使它有点基本。



我是什么尝试过:



I've been working on a project, and it requires me to generate a toString() in a Double Linked List Interface. We could use a StringBuilder, and appending method, but I wanted to make it a bit basic by just returning them without any inbuilt java method.

What I have tried:

@Override
	public String toString()
	{
		String result = "";
		DoubleLinkedNode currentNode = this.firstNode;
		while(currentNode != null) {
			System.out.print(current.data + ",  "); 
			result += currentNode.data.toString() +" , ";
			currentNode = currentNode.next;
		}
		String now = " .";
		
		
		return result + now; // this is returned in case bag is empty.
		
		
	}









预计:





Expected :

"a", "q", "%", "asdf"



输出:


Output:

asdf,  123,  q,  a,  asdf , 123 , q , a ,  .

推荐答案

引用:

他们说我们可以使用StringBuilder和追加方法,但是我想通过返回它们而没有任何内置的java方法来使它有点基础

They said that we could use a StringBuilder, and appending method, but I wanted to make it a bit basic by just returning them without any inbuilt java method

它们是对的,然而。

使用StringBuilder可以使您的代码更加清晰,高效并且可能正确。

请参阅:例如: []。

They are right, however.
Using the StringBuilder would make your code more clear, efficient and possibly correct.
See, for instance: string - When to use StringBuilder in Java - Stack Overflow[^].


这篇关于我们如何创建双链表节点到以逗号分隔的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:13