链表数组实现- 打印方法无法编译

huangapple go评论85阅读模式
英文:

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 &lt; 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 &gt; 0; i--)
arrayVal[i] = arrayVal[i - 1];
arrayVal[0] = value;
size++;
}else
System.out.println(&quot;The List is Full.&quot;);
}
public void addTail(int value){
if(! this.isFull()){
arrayVal[size] = value;
size++;
}else
System.out.println(&quot;The List is Full.&quot;);
}
public int removeHead() {
if(! this.isEmpty())
{
int value = arrayVal[0];
for(int i = 0; i &lt; size -1; i++){
arrayVal[i] = arrayVal[i+1];
}
size--;
return value;
}else 
System.out.println(&quot;The List is Empty.&quot;);
return arrayVal[size];
}
public int removeTail(){
if(!isEmpty()){ 
size--; 
}else 
System.out.println(&quot;The List is Empty.&quot;);
return arrayVal[size];
}
public String printList(){
String listString = new String();
if(! this.isEmpty())
return &quot;The List is Empty.&quot;;
for(int i = 0; i &lt; size; i++) {
listString += arrayVal[i];
if(i &lt; size -1) {
listString += &quot;, &quot;;
}
}  
return &quot;Elements in the List: &quot; + listString;
} 
}

Driver Class

  public class ProgramDriver {
public static void main(String[] args) {
LinkedListArray lla = new LinkedListArray(10);
System.out.println(&quot;Size of List: &quot; + lla.size());
System.out.println(&quot;Is the list empty?: &quot; + lla.isEmpty());
System.out.println(&quot;Is the list full?: &quot; + 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(&quot;Size of List: &quot; + lla.size());
System.out.println(&quot;Is the list empty?: &quot; + lla.isEmpty());
System.out.println(&quot;Is the list full?: &quot; + lla.isFull());
lla.removeHead();
lla.removeTail();
System.out.println(&quot;Size of List: &quot; + lla.size());
System.out.println(&quot;Is the list empty?: &quot; + lla.isEmpty());
System.out.println(&quot;Is the list full?: &quot; + lla.isFull());
lla.printList();  
}
}

答案1

得分: 0

首先:StackOverflow 不是一个代码审查网站。不要只是发布代码然后询问别人的看法。(可以查看 https://codereview.stackexchange.com/

因此,在提出诸如这样的问题时,明确指定您看到的行为,以及它与您预期的方式有何不同,这将非常有帮助。这样,我们可以帮助您解决您实际遇到的具体问题。

话虽如此,以下显然是一个问题:

       if(! this.isEmpty())
           return &quot;The List is Empty.&quot;;

如果列表不为空,您为什么要说“列表是空的。”?我猜想您想要删除那里的 ! 符号。

除此之外,在循环中进行字符串拼接可能会变得昂贵,特别是当列表中有大量项目时。您应该避免这种情况。可以考虑使用 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 &quot;The List is Empty.&quot;;

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.

huangapple
  • 本文由 发表于 2020年10月20日 23:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定