首页 > 解决方案 > LinkedList 与 if 语句?

问题描述

所以我有这个任务要做,关于LinkedList,你可以看看我的主文件,还必须提到我的“//条件”部分是错误的,我只是把一些东西作为一个想法,但实际上不是真的工作

import java.util.*;

public class Main {
    public static void main(String[] args){
    
        ArrayList nokiaAL = new ArrayList();
        LinkedList phoneAL = new LinkedList();
        
        //input
        Smartphone a = new Smartphone("Nokia","Nokia 7 Plus",1300,260101);
        Smartphone b = new Smartphone("Samsung","Galaxy S8",900,220100);
        Smartphone c = new Smartphone("Xiaomi","Mi 10",1500,150031);
        Smartphone d = new Smartphone("Nokia","3310",250,101001);
        Smartphone e = new Smartphone("Samsung","Galaxy Y",400,774101);
        Smartphone f = new Smartphone("Apple","iPhone 7",1100,316300);
        
        phoneAL.insertAtFront(f);
        phoneAL.insertAtFront(e);
        phoneAL.insertAtFront(d);
        phoneAL.insertAtFront(c);
        phoneAL.insertAtFront(b);
        phoneAL.insertAtFront(a);
        
        //process
        Object r = (Object) phoneAL.getFirst();
        while (r != null) {
            System.out.print(" "+r);
            r = (Object) phoneAL.getNext();
        }
        
        //The conditions
        //If nokia + the price $1200+, it will save all the info about nokia
        //If brand samsung + model Galaxy Y, It will count the total of the phone
        Object obj;
        int countSamsung = 0;
        for(int i=0;i<phoneAL.size();i++){
            obj = phoneAL.get(i);
            Smartphone obj2 = (Smartphone) obj;
            if(obj2.getBrand().equalsIgnoreCase("Nokia")){
                nokiaAL.add(obj2);
            }
            if(obj2.getBrand().equalsIgnoreCase("Samsung")){
                if(obj2.getModel().equalsIgnoreCase("Galaxy Y")){
                    countSamsung++;
                }
            }
        }
        
        //output
        System.out.println("\n");
        System.out.println("Details about Nokia phone more than RM1200:"+nokiaAL.toString());
        System.out.println("Quantity of Samsung model Galaxy Y: " + countSamsung);
    }
}

我知道如何打印LinkedList中的所有细节,这里的重点是,你不能添加或更改其他.java文件的任何内容,你只能编辑Main.java文件,这可能吗?这是我的智能手机和 LinkedList 代码。


public class Smartphone {
    
    String brand;//e.g: Nokia, Samsung
    String model;//e.g: Lumia, Galaxy Y, Note S
    double price;
    int warranty;//warranty (in year)

    Smartphone() {
        
    }
    
public Smartphone(String a, String b, double c, int d){
    this.brand=a;
    this.model=b;
    this.price=c;
    this.warranty=d;
}    

public String getBrand(){
    return brand;
}

public String getModel(){
    return model;
}

public double getPrice(){
    return price;
}

public int getWarranty(){
    return warranty;
}

public String toString(){
    return "\n\nBrand: "+brand +"\nModel: "+ model +"\nPrice: $"+ price +"\nWarranty: "+ warranty;
    }
}

public class LinkedList 
{
   private Node first;
   private Node last;
   private Node current; 
   
    public LinkedList()
    {
        first = null;
                last = null;
                current = null;  
    }
    
    public boolean isEmpty(){   
            return (first == null); }   
    
    public void insertAtFront(Object insertItem){
            Node newNode = new Node(insertItem);
       
      
            if (isEmpty()){
        first = newNode;
        last = newNode;
      }else{
          newNode.next = first;
          first = newNode;
      }
    }
        public void insertAtBack(Object insertItem){
            Node newNode = new Node(insertItem);
        
        if(isEmpty()){
            first = newNode;
            last = newNode;
        }else{
            last.next = newNode;
            last = newNode;
        }
    }
        public Object removeFromFront(){
            Object removeItem = null;
        
        if(isEmpty()){
            return removeItem;
        }
        
        removeItem = first.data;
        
          if(first == last){
            first = null;
            last = null;
            }else
            first = first.next;
           
    return removeItem;
    }
    
    public Object removeFromBack(){
    Object removeItem = null;
        
      if(isEmpty())
      {
        return removeItem;
      }
        
      removeItem = last.data;
          
        if (first == last)
      {
         first = null;
         last = null;
      }else{
         current = first;
         while(current.next != last)
            current = current.next;
         last = current;
         last.next = null;
        }
        
      return removeItem;
     }
     
     public Object getFirst(){
        if(isEmpty())
          return null;
        else
        {
            current = first;
            return current.data;
        }
    }
     
     public Object getNext(){
        if(current == last)
          return null;
        else
        {
            current = current.next;
            return current.data;
        }
    }    
}

正如我之前所说,我可以打印手机的所有详细信息,但如何真正将其用作条件,如 If-else 语句?例如, if(obj.getBrand().equalsIgnoreCase("Nokia")){} ,我可以使用 ArrayList 来实现这一点,但由于这是 LinkedList 任务,所以我仍然在不知道它是否可能的情况下解决这个问题. 我希望有人能理解这一点并能够提供帮助。TQ

这是我的 LinkedList 的节点代码


public class Node {
    Object data;
    Node next;
    
    Node(Object obj){
        data=obj;
    }
}

标签: javalistif-statement

解决方案


您应该使用 while 进行迭代并验证列表是否已结束。与 ArrayList 不同,您可以直接访问向量位置,在链表中,您应该从一个节点走到另一个节点。此外,在您的示例中,您只实现了一个getNext()方法,而不是一个get(i).

例子:

Object aux = linkedList.getFirst();
while(aux != null) {
  // your business logic here
  aux = linkedList.getNext();
}

由于您没有在实现中使用泛型,因此要访问您的对象数据,您将需要在实现中使用强制转换或使用泛型。

施法方式:

while(aux != null) {
  phoneObject = (Smartphone) aux;
  // your business logic here
  if(phoneObject.getBrand().equalsIgnoreCase("Nokia")){
    System.out.println("Phone brand == Nokia");
  }
  aux = linkedList.getNext();
}

在通用方法中,您还需要更改 LinkedList 实现和 Node 实现。

链表:

public class LinkedList<T>
{
   private Node<T> first;
   private Node<T> last;
   private Node<T> current; 

   public T getFirst(){
        if(isEmpty())
          return null;
        else
        {
            current = first;
            return current.data;
        }
    }

    public T getNext(){
        if(current == last)
          return null;
        else
        {
            current = current.next;
            return current.data;
        }
    }

    // add other methods here
}

节点:

public class Node<T> {
    T data;
    Node<T> next;

    // add other methods here
}

主要的:

LinkedList<Smartphone> linkedList = new LinkedList<Smartphone>();

// add objects

Smartphone aux = linkedList.getFirst();
while(aux != null) {
  // no need to cast, because of generics use
  if(aux.getBrand().equalsIgnoreCase("Nokia")){
    System.out.println("Phone brand == Nokia");
  }
  // your business logic here
  aux = linkedList.getNext();
}

如果您的列表已结束,则您的方法返回,因此我们的停止条件getNext()是. 我们的循环将在 aux 不为空时执行,执行所有业务逻辑(if 子句或您想要执行的任何验证),并在循环结束时,将下一个对象设置为变量。nullaux == nullaux


推荐阅读