简体   繁体   English

arraylist找不到符号:方法addLast

[英]arraylist cannot find symbol: method addLast

I'm having a problem with using the addLast method after declaring an ArrayList. 我在声明ArrayList之后使用addLast方法遇到问题。 Here's the class code: 这是课程代码:

import java.util.*;

public class Neuron{

        public int m_numInputs;
        public ArrayList<Double> m_vecWeight = new ArrayList<Double>();

        public Neuron(int numInputs){
                this.m_numInputs = numInputs + 1;

                //additional weight for bias
                for(int i = 0; i < numInputs + 1; ++i){
                        Random rand = new Random();
                        m_vecWeight.addLast(rand.nextFloat() * 2.0 - 1.0);

                }
        }

}

So the error I'm getting is: 所以我得到的错误是:

cannot find symbol: method addLast(double), location: variable m_vecWeight of type ArrayList<Double>

Any guidance would be greatly appreciated. 任何指导将不胜感激。

The addLast() method exists in the LinkedList class, not in ArrayList . addLast()方法存在于LinkedList类中,而不存在于ArrayList You can either: 您可以:

  1. Switch to use a LinkedList or 切换为使用LinkedList
  2. Simply call add() . 只需调用add()

As stated in the documentation , addLast() is equivalent to add() . 文档中所述, addLast()等同于add()

Java ArrayList don't have addLast() method. Java ArrayList没有addLast()方法。

Use add() method from ArrayList to add in a specified index or use LinkedList which has addLast() method. 使用ArrayList add()方法添加指定的索引,或者使用具有addLast()方法的LinkedList

Appends the specified element to the end of this list. 将指定的元素追加到此列表的末尾。

This method is equivalent to add(E) . 此方法等效于add(E)

You need to use add() method to add the element in the arraylist 您需要使用add()方法将元素添加到arraylist中

change m_vecWeight.addLast(rand.nextFloat() * 2.0 - 1.0); 更改m_vecWeight.addLast(rand.nextFloat() * 2.0 - 1.0); to

 m_vecWeight.add(rand.nextFloat() * 2.0 - 1.0);

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

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