简体   繁体   English

Main.java使用未经检查或不安全的操作

[英]Main.java uses unchecked or unsafe operations

I tried to implement my own singly linkedList in java and I wanted it to be generic because I think it's the best choice. 我尝试在Java中实现自己的单链接列表,我希望它是通用的,因为我认为这是最佳选择。 Then, I want to write a specific function to sum two linkedlist of integer. 然后,我想编写一个特定的函数来求和两个整数的链表。 The problem is due to the genericity of the list, I got an error and I can't check my code... 问题是由于列表的通用性,出现错误,我无法检查代码...

Here is the code of my function : 这是我的功能的代码:

public LinkedList<Integer> sum(LinkedList<Integer> list1, LinkedList<Integer> list2){
//  if (list1== null || list2 ==null) return null;

    LinkedList<Integer> result = new LinkedList<Integer>();

    Node<Integer> node11 = list1.first;
    Node<Integer> node22 = list2.first;
    int temp = 0;

    while (node11.next!=null && node22.next!=null){
            int node1 = node11.data;
            int node2 = node22.data;
            int node3 = (node1 + node2 + temp) %10;
            temp = (node1 + node2)>=10 ? 1: 0;
            result.insertLast(node3);

            node11 = node11.next;
            node22 = node22.next;
    }

    return result;

             }

There is the code for my two classes : Node and linkedList 我的两个类都有代码:Node和linkedList

class Node<T> {
  public T data;                // data in Node.
  public Node<T> next;          // points to next Node in list.

  public Node(T data){                                       
  this.data = data;
  }
}

class LinkedList<T> {
public Node<T> first;       

public LinkedList(){
      first = null;
}

 public void insertLast(T data){
      Node<T>  newNode = new Node<T> (data);        //Creation of New Node.

      if(first==null)   {
             first=newNode;                 
             return;
      }

      Node<T>  tempNode = first;
            while(tempNode.next!=null){
            tempNode=tempNode.next;
            }
      tempNode.next=newNode; 
      }

And here is my main when I wanted to try this code : 当我想尝试以下代码时,这是我的主要知识:

    LinkedList<Integer> list = new LinkedList();
    LinkedList<Integer> list2 = new LinkedList();

    list.insertLast(5);
    list.insertLast(1);
    list.insertLast(3);    

    list2.insertLast(2);
    list2.insertLast(9);
    list2.insertLast(5); 

    LinkedList<Integer> list3 = sum(list, list2);

And there is the error that I get (i'm using ideone as an IDE) : 而且有我得到的错误(我使用ideone作为IDE):

     Main.java:179: error: cannot find symbol
    LinkedList<Integer> list3 = sum(list, list2);
                                ^
     symbol:   method sum(LinkedList<Integer>,LinkedList<Integer>)
     location: class Ideone
     Note: Main.java uses unchecked or unsafe operations.
     Note: Recompile with -Xlint:unchecked for details.

Can you please tell me where is the error and what I should do to correct that? 您能告诉我错误在哪里,我应该怎么做才能纠正错误? I know it's due to genericity (because I had to only use Integer in my function). 我知道这是由于通用性(因为我只需要在函数中使用Integer)。 I tried a looot of things but none of them worked... 我尝试了很多事情,但没有一个起作用。

Thanks! 谢谢!

Use the keywork static in your sum method : 在sum方法中使用keywork static:

public static LinkedList sum(LinkedList list1, LinkedList list2){ ... } 公共静态LinkedList sum(LinkedList list1,LinkedList list2){...}

if you call sum from static main method 如果您从静态main方法调用sum

It looks like you are adding two big integers, each is represented by a list. 好像您要添加两个大整数,每个整数都由一个列表表示。

There are two errors: a). 有两个错误:a)。 in function "sum", the termination condition of while loop is incorrect. 在函数“ sum”中,while循环的终止条件不正确。 The last two elements are always not added. 始终不添加最后两个元素。 b). b)。 in function "sum" temp represents whether there's an increase to the previous position. 函数“ sum”中的temp表示上一个位置是否增加。 It's not handled. 没有处理。

Consider BigInteger or java built-in List? 考虑使用BigInteger还是Java内置列表?

public class ThreadingMulti {
    public static void main(String[] args) throws InterruptedException {
        LinkedList<Integer> list = new LinkedList();
        LinkedList<Integer> list2 = new LinkedList();

        list.add(5);
        list.add(1);
        list.add(3);    

        list2.add(2);
        list2.add(9);
        list2.add(5); 

        LinkedList<Integer> list3 = sum(list, list2);
    }

    public static LinkedList<Integer> sum(LinkedList<Integer> list1, LinkedList<Integer> list2){
        LinkedList<Integer> result = new LinkedList<Integer>();
        return result;

    }
}

This code is working fine. 这段代码工作正常。 Seems problem with sum method type (static). 求和方法类型(静态)似乎存在问题。 Please verify whether you might be calling non static method from static method. 请验证您是否正在从静态方法调用非静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM