LinkedList 是一種線性數據結構,其中元素不存儲在連續的內存位置。
為了將鏈表轉換為字符串,我們需要遍曆鏈表,然後,我們需要將鏈表的元素附加到字符串變量。
我們可以使用 String 類、StringBuilder 或 StringBuffer 將 LinkedList 轉換為字符串。
對於附加字符串,我們可以對 String 類使用 “+” 運算符,對 StringBuffer 和 StringBuilder 使用 append 方法。
範例1:
Java
// Java program to Convert LinkedList to String in Java
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
class GFG {
public static void main(String[] args)
{
// creating a linkedlist
LinkedList<Integer> list = new LinkedList<>();
// adding elements to linkedlist
list.add(100);
list.add(200);
list.add(300);
list.add(400);
// By using String Class
String s = "";
// using iterator for traversing a linkedllist
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
// appending using "+" operator
s = s + iterator.next() + " ";
}
// Displaying output
System.out.println(s);
// By using String Buffer
StringBuffer stringBuffer = new StringBuffer();
// using enhanced for loop for traversing a
// linkedlist
for (Integer integer:list) {
// using append method for appending string
stringBuffer.append(integer).append(" ");
}
// displaying output
System.out.println(stringBuffer);
// By using String Builder
StringBuilder stringBuilder = new StringBuilder();
// using ListIterator for traversing a linked list
ListIterator<Integer> listIterator
= list.listIterator();
while (listIterator.hasNext()) {
// using append method for appending string
stringBuilder.append(listIterator.next())
.append(" ");
}
// displaying output
System.out.println(stringBuilder);
}
}
輸出
100 200 300 400 100 200 300 400 100 200 300 400
範例2:在這個例子中,我們將使用一個用戶定義的類
Java
// Java program to Convert LinkedList to String in Java
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
class friendsDetail {
// class field
private String name;
private String nickName;
private int id;
// parameterised constructor
public friendsDetail(String name, String nickName,
int id)
{
this.name = name;
this.nickName = nickName;
this.id = id;
}
// getter for name
public String getName() { return name; }
// setter for name
public void setName(String name) { this.name = name; }
// getter for marks
public int getID() { return id; }
// setter for marks
public void setID(int id) { this.id = id; }
// getter for nickname
public String getNickName() { return nickName; }
// setter for nickname
public void setNickName(String nickName)
{
this.nickName = nickName;
}
//@Override toString method
public String toString()
{
return "name='" + name + '\'' + ", nickName='"
+ nickName + '\'' + ", ID=" + id;
}
}
class Main {
public static void main(String[] args)
{
LinkedList<friendsDetail> list = new LinkedList<>();
// adding elements to linked list
list.add(
new friendsDetail("Ridhi", "Goda", 202199));
list.add(
new friendsDetail("Ananya", "Natki", 202195));
list.add(new friendsDetail("Ankit", "Chota Don",
202192));
list.add(
new friendsDetail("Suraj", "Bhindi", 202147));
list.add(
new friendsDetail("Sarthak", "Nagin", 202178));
list.add(new friendsDetail("Sonika", "Chipkali",
202167));
list.add(new friendsDetail("Shishya", "Gorilla",
202157));
System.out.println("Using String Class\n");
// using string class
String s = "";
// using iterator for traversing a linked list
Iterator<friendsDetail> itr = list.iterator();
while (itr.hasNext()) {
// appending string using "+" operator
s = s + itr.next() + "\n";
}
// Displaying output
System.out.println(s);
System.out.println("\nUsing String Buffer\n");
// using string buffer
StringBuffer stringBuffer = new StringBuffer();
// using enhanced for loop for linked list traversal
for (friendsDetail friendmarks:list) {
// using append method for appending
stringBuffer.append(friendmarks).append("\n");
}
// displaying output
System.out.println(stringBuffer);
// using string builder
System.out.println("\nUsing String Builder\n");
// using list iterator for traversing a linked list
ListIterator<friendsDetail> itr2
= list.listIterator();
StringBuilder stringBuilder = new StringBuilder();
while (itr2.hasNext()) {
// using append method for appending
stringBuilder.append(itr2.next()).append("\n");
}
// displaying output
System.out.println(stringBuilder);
}
}
輸出
Using String Class name='Ridhi', nickName='Goda', ID=202199 name='Ananya', nickName='Natki', ID=202195 name='Ankit', nickName='Chota Don', ID=202192 name='Suraj', nickName='Bhindi', ID=202147 name='Sarthak', nickName='Nagin', ID=202178 name='Sonika', nickName='Chipkali', ID=202167 name='Shishya', nickName='Gorilla', ID=202157 Using String Buffer name='Ridhi', nickName='Goda', ID=202199 name='Ananya', nickName='Natki', ID=202195 name='Ankit', nickName='Chota Don', ID=202192 name='Suraj', nickName='Bhindi', ID=202147 name='Sarthak', nickName='Nagin', ID=202178 name='Sonika', nickName='Chipkali', ID=202167 name='Shishya', nickName='Gorilla', ID=202157 Using String Builder name='Ridhi', nickName='Goda', ID=202199 name='Ananya', nickName='Natki', ID=202195 name='Ankit', nickName='Chota Don', ID=202192 name='Suraj', nickName='Bhindi', ID=202147 name='Sarthak', nickName='Nagin', ID=202178 name='Sonika', nickName='Chipkali', ID=202167 name='Shishya', nickName='Gorilla', ID=202157
相關用法
- Java ArrayList轉LinkedList用法及代碼示例
- Java LinkedList轉Array用法及代碼示例
- Java Array轉LinkedList用法及代碼示例
- Java HashMap轉LinkedList用法及代碼示例
- Java LinkedList removeFirst()用法及代碼示例
- Java LinkedList getFirst()用法及代碼示例
- Java LinkedList getLast()用法及代碼示例
- Java LinkedList clear()用法及代碼示例
- Java LinkedList addFirst()用法及代碼示例
- Java LinkedList addLast()用法及代碼示例
- Java LinkedList addAll()用法及代碼示例
- Java LinkedList set()用法及代碼示例
- Java LinkedList removeLast()用法及代碼示例
- Java LinkedList get()用法及代碼示例
- Java LinkedList size()用法及代碼示例
- Java LinkedList indexOf()用法及代碼示例
注:本文由純淨天空篩選整理自le0大神的英文原創作品 Convert LinkedList to String in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。