英文:
Linked List Array Based Implementation- Print Method Not Compiling
问题
以下是你提供的代码的翻译部分:
链表类:
public class LinkedListArray {
int size;
int maxSize;
int arrayVal[];
public LinkedListArray()
{
this.size = 10;
}
public LinkedListArray(int list)
{
if(list < 0)
maxSize = 20;
else
maxSize = list;
size = 0;
arrayVal = new int[maxSize];
}
// 其他方法的翻译...
public String printList(){
String listString = new String();
if(! this.isEmpty())
return "列表为空。";
for(int i = 0; i < size; i++) {
listString += arrayVal[i];
if(i < size - 1) {
listString += ", ";
}
}
return "列表中的元素: " + listString;
}
}
驱动类:
public class ProgramDriver {
public static void main(String[] args) {
LinkedListArray lla = new LinkedListArray(10);
System.out.println("列表大小: " + lla.size());
System.out.println("列表是否为空?: " + lla.isEmpty());
System.out.println("列表是否已满?: " + lla.isFull());
lla.addHead(4);
lla.addHead(5);
lla.addTail(10);
lla.addTail(9);
lla.addTail(7);
lla.addTail(2);
lla.addHead(8);
lla.addHead(1);
lla.addTail(3);
lla.addTail(6);
System.out.println(lla.printList());
System.out.println("列表大小: " + lla.size());
System.out.println("列表是否为空?: " + lla.isEmpty());
System.out.println("列表是否已满?: " + lla.isFull());
lla.removeHead();
lla.removeTail();
System.out.println("列表大小: " + lla.size());
System.out.println("列表是否为空?: " + lla.isEmpty());
System.out.println("列表是否已满?: " + lla.isFull());
System.out.println(lla.printList());
}
}
注意:由于你要求只返回翻译好的部分,所以我只提供了代码部分的翻译。如有需要,你可以将这些翻译放入你的程序中进行测试。
英文:
For an Assignment we had previously implemented An Array Based Linked List using a node class however, she has requested all students redo this assignment without using the Node class. I have everything working as intended except for my printList Method- Can you please review my printList Method and let me know your thoughts? Below is the class and the driver.
Linked List Class
public class LinkedListArray {
int size;
int maxSize;
int arrayVal[];
public LinkedListArray()
{
this.size = 10;
}
public LinkedListArray(int list)
{
if(list < 0)
maxSize = 20;
else
maxSize = list;
size = 0;
arrayVal = new int[maxSize];
}
public int size(){
return size;
}
public boolean isEmpty() {
return (size() == 0);
}
public boolean isFull(){
return (size() == maxSize);
}
public void addHead(int value){
if(! this.isFull()){
for(int i = size; i > 0; i--)
arrayVal[i] = arrayVal[i - 1];
arrayVal[0] = value;
size++;
}else
System.out.println("The List is Full.");
}
public void addTail(int value){
if(! this.isFull()){
arrayVal[size] = value;
size++;
}else
System.out.println("The List is Full.");
}
public int removeHead() {
if(! this.isEmpty())
{
int value = arrayVal[0];
for(int i = 0; i < size -1; i++){
arrayVal[i] = arrayVal[i+1];
}
size--;
return value;
}else
System.out.println("The List is Empty.");
return arrayVal[size];
}
public int removeTail(){
if(!isEmpty()){
size--;
}else
System.out.println("The List is Empty.");
return arrayVal[size];
}
public String printList(){
String listString = new String();
if(! this.isEmpty())
return "The List is Empty.";
for(int i = 0; i < size; i++) {
listString += arrayVal[i];
if(i < size -1) {
listString += ", ";
}
}
return "Elements in the List: " + listString;
}
}
Driver Class
public class ProgramDriver {
public static void main(String[] args) {
LinkedListArray lla = new LinkedListArray(10);
System.out.println("Size of List: " + lla.size());
System.out.println("Is the list empty?: " + lla.isEmpty());
System.out.println("Is the list full?: " + lla.isFull());
lla.addHead(4);
lla.addHead(5);
lla.addTail(10);
lla.addTail(9);
lla.addTail(7);
lla.addTail(2);
lla.addHead(8);
lla.addHead(1);
lla.addTail(3);
lla.addTail(6);
lla.printList();
System.out.println("Size of List: " + lla.size());
System.out.println("Is the list empty?: " + lla.isEmpty());
System.out.println("Is the list full?: " + lla.isFull());
lla.removeHead();
lla.removeTail();
System.out.println("Size of List: " + lla.size());
System.out.println("Is the list empty?: " + lla.isEmpty());
System.out.println("Is the list full?: " + lla.isFull());
lla.printList();
}
}
答案1
得分: 0
首先:StackOverflow 不是一个代码审查网站。不要只是发布代码然后询问别人的看法。(可以查看 https://codereview.stackexchange.com/ )
因此,在提出诸如这样的问题时,明确指定您看到的行为,以及它与您预期的方式有何不同,这将非常有帮助。这样,我们可以帮助您解决您实际遇到的具体问题。
话虽如此,以下显然是一个问题:
if(! this.isEmpty())
return "The List is Empty.";
如果列表不为空,您为什么要说“列表是空的。”?我猜想您想要删除那里的 !
符号。
除此之外,在循环中进行字符串拼接可能会变得昂贵,特别是当列表中有大量项目时。您应该避免这种情况。可以考虑使用 String.join()
或者 StringBuilder
。
英文:
First: StackOverflow isn't a code review site. Don't just post code and ask people what they think. (Look into https://codereview.stackexchange.com/ for that)
So when asking a question like this, it's very helpful to specify what behavior you're seeing, and in what way it's not what you expect. That way we can help you solve the specific problem you're actually running into.
That said, this is obviously a problem:
if(! this.isEmpty())
return "The List is Empty.";
If the list is not empty, why would you say "The List is Empty."? I'm guessing you want to remove the !
there.
Apart from that, string concatenation in a loop can get expensive if you end up with a lot of items in your list. You should avoid it. Look into String.join()
or StringBuilder
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论