我想使用一個簡單的回圈為每次迭代添加一個行號。
我希望顯示適合用戶觀看。
@Override
public String toString() {
return String.format(Locale.US, "s s %-10s .2f ",
this.firstName, this.lastName,this.age, this.length);
}
我想要的輸出
Nr Name Age Length[M]
1 Mona Beckham 23 1.80
2 John Robinhood 23 1.80
我得到的輸出
Mona Beckham 23 1.80
John Robinhood 23 1.80
uj5u.com熱心網友回復:
您可以使用的方法數量幾乎是無限的。下面是一個演示Person類,其中包含一個名為的方法,該方法toTableString()接受兩個特定的引數。第一個引數是person(List<Person>)的串列界面,它將包含要在表型別格式中列印到控制臺的人的實體。第二個引數是布林值,是可選的。它確定該方法是否還應為回傳的表格型別格式化字串提供帶下劃線的標題行。默認情況下,這是 false,因此如果沒有為此引數提供任何內容或提供 false,則不提供 Header 行。但是,如果提供布林值 true,則提供帶下劃線的標題行。
這只是一個例子。你可以使用這么多方法:
演示人類課程:
public class Person {
private String firstName;
private String lastName;
private int age;
private double length;
public Person() { }
public Person (String firstName, String lastName, int age, double length) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.length = length;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public String toString() {
return firstName ", " lastName ", " age ", " length;
}
/**
* Returns a string which lists the supplied instances of Person in a table
* type format.<br><br>
*
* @param personsList (List Interface of Person) A list of Person instances
* to convert to table type format.<br>
*
* @param applyHeader (Optional - boolean) Default is boolean false whereas
* no header and header underline is supplied within the returned String. If
* boolean true is supplied then a Header line describing the column names
* and an underline is also included within the returned table type formated
* String.<br>
*
* @return (String) A String with instances of Person data formated in a
* table type format.
*/
public String toTableString(java.util.List<Person> personsList, boolean... applyHeader) {
String ls = System.lineSeparator();
boolean addHeader = false;
if(applyHeader.length > 0) {
addHeader = applyHeader[0];
}
StringBuilder sb = new StringBuilder("");
if (addHeader) {
String header = String.format(java.util.Locale.US, "%-8s %-20s %-6s s",
"No.", "Name", "Age", "Length[m]") ls;
String underline = String.join("", java.util.Collections.nCopies(header.length()-2, "=")) ls;
sb.append(header).append(underline);
}
for (int i = 0; i < personsList.size(); i ) {
String name = personsList.get(i).firstName " " personsList.get(i).lastName;
sb.append(String.format(java.util.Locale.US, "%-8d %-20s %-6d %7.2f %n",
(i 1), name, personsList.get(i).age, personsList.get(i).length));
}
return sb.toString();
}
}
您如何使用該toTableString()方法:
Person persons = new Person();
List<Person> list = new ArrayList<>();
list.add(new Person("Mona", "Beckham", 23, 1.80));
list.add(new Person("John", "Robinhood", 23, 1.91));
list.add(new Person("Fred", "Flintstone", 36, 1.72));
System.out.println("Without Header Line...");
System.out.println();
System.out.println(persons.toTableString(list));
System.out.println();
System.out.println("With Header Line...");
System.out.println();
System.out.println(new Person().toTableString(list, true));
和控制臺視窗應顯示:
Without Header Line...
1 Mona Beckham 23 1.80
2 John Robinhood 23 1.91
3 Fred Flintstone 36 1.72
With Header Line...
No. Name Age Length[m]
===============================================
1 Mona Beckham 23 1.80
2 John Robinhood 23 1.91
3 Fred Flintstone 36 1.72
uj5u.com熱心網友回復:
在這種情況下,一種方法是使用串列和I 每次曝氣創建一個名為PrintList的方法。
ListIterator iterator = list.listIterator();
int i = 1;
while (iterator.hasNext()){
System.out.println(i "\t" iterator.next());
i ;
}
```
uj5u.com熱心網友回復:
您也可以嘗試保留一個名為COUNT的全域變數,并每次增加它的計數。當然,相應地更改字串格式。
int count = 1;
@Override
public String toString() {
return String.format(Locale.US, "s s %-10s .2f ",
count , this.firstName, this.lastName,this.age, this.length);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/441590.html
上一篇:腳本創建的圓圈的隨機移動
