gpt4 book ai didi

java - 为什么在打印大型 ArrayLists 时会出现奇怪的字符

转载 作者:行者123 更新时间:2023-11-30 07:52:03 24 4
gpt4 key购买 nike

我正在学习部分涉及打印 ArrayLists 元素的教程。该程序在处理小列表时完全按照我的预期运行。但是,当输入较大的数字时,字符串格式(我相信)会导致一些奇怪的结果。

我的代码如下:

public class Theatre {
private final String theatreName;
public List<Seat> seats = new ArrayList<>();

public Theatre(String theatreName, int numRows, int seatsPerRow) {
this.theatreName = theatreName;

int lastRow = 'A' + (numRows -1);
for (char row = 'A'; row <= lastRow; row++) {
for(int seatNum = 1; seatNum <= seatsPerRow; seatNum++) {
Seat seat = new Seat(row + String.format("%02d", seatNum));
seats.add(seat);
}
}
}

public String getTheatreName() {
return theatreName;
}

public boolean reserveSeat(String seatNumber) {
int low = 0;
int high = seats.size()-1;

while(low <= high) {
System.out.print(".");
int mid = (low + high) /2;
Seat midVal = seats.get(mid);
int cmp = midVal.getSeatNumber().compareTo(seatNumber);

if(cmp <0) {
low = mid + 1;
} else if(cmp > 0) {
high = mid -1;
} else {
return seats.get(mid).reserve();
}
}

System.out.println("There is no seat " + seatNumber);
return false;


}

// for testing
public void getSeats() {
for(Seat seat : seats) {
System.out.println(seat.getSeatNumber());
}
}

public class Seat implements Comparable<Seat > {
private final String seatNumber;
private boolean reserved = false;

public Seat(String seatNumber) {
this.seatNumber = seatNumber;
}

public boolean reserve() {
if(!this.reserved) {
this.reserved = true;
System.out.println("Seat " + seatNumber + " reserved");
return true;
} else {
return false;
}
}

public boolean cancel() {
if(this.reserved) {
this.reserved = false;
System.out.println("Reservation of seat " + seatNumber + " cancelled");
return true;
} else {
return false;
}
}

public String getSeatNumber() {
return seatNumber;
}

@Override
public int compareTo(Seat seat) {
// returns integer greater than 0 if greater than, less than if less than, 0 if equal
return this.seatNumber.compareTo(seat.getSeatNumber());
}
}

使用 Main 方法类:

public static void main(String[] args) {
Theatre theatre = new Theatre("Olympian", 800, 12);
List<Theatre.Seat> seatCopy = new ArrayList<>(theatre.seats); // shallow copy, contains references to all
// elements of both lists, original and copy
printList(seatCopy);


seatCopy.get(1).reserve();
if (theatre.reserveSeat("A02")) {
System.out.println("Please pay for A02");
} else {
System.out.println("seat already reserved");
}

// see that they are clearly two separate array lists
Collections.reverse(seatCopy);
System.out.println("Printing seat copy");
printList(seatCopy);
System.out.println("Printing theatre.seats");
printList(theatre.seats);
System.out.println("Shuffling seatCopy");
Collections.shuffle(seatCopy);
printList(seatCopy);

}

public static void printList(List<Theatre.Seat> list) {
for (Theatre.Seat seat : list) {
System.out.print(" " + seat.getSeatNumber());
}
System.out.println();
System.out.println("===============================");
}

}

输出(我只引用够看)是:

12 ͠11 ͠10 ͠09 ͠08 ͠07 ͠06 ͠05 ͠04 ͠03 ͠02 ͠01 ͟12 ͟
===============================
Printing theatre.seats
A01 A02 A03 A04 A05 A06 A07 A08 A09 A10
===============================
===============================
Shuffling seatCopy
V07 Ý11 11 ű05 Ú02 ̄06 ̓01 ŕ12 ȣ03 Ǔ05 S07

我知道我用完了字母字符并且此行中的格式为:

Seat seat = new Seat(row + String.format("%02d", seatNum));

仅用于处理格式为“X##”的席位。

我想了解的是奇怪字符出现的具体原因(“~”和“'”等)。显然,格式不合适。但为什么它会专门产生这种输出?

谢谢你的帮助,马克

最佳答案

你自己说的。您的字母字符用完了。事实上,您完全用完了 ASCII 字符。从这一行:

for (char row = 'A'; row <= lastRow; row++)

您正在做的是从“A”开始行字母并继续跨越 Unicode 字符集。因此,如果超过 26 行,您将开始得到像 ~ 这样的符号,而如果行数足够多,您将完全不使用 ASCII 并开始得到奇怪的行字母,例如 Ý。

如果您不希望这种情况发生,则需要放弃 for 循环并提出一种完全不同(且更复杂)的分配行标签的方法。

关于java - 为什么在打印大型 ArrayLists 时会出现奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com