简体   繁体   English

使用for循环将双精度数组值添加到arraylist

[英]Adding double array values to an arraylist using a for loop

I apologize in advance if I am making any mistakes asking this question. 如果我在问这个问题时有任何错误,我事先表示歉意。 I am new to stackoverflow and java. 我是Stackoverflow和Java的新手。

My problem is that I want to be able to convert an ordinary array of doubles to an arraylist, I need to operate on this arraylist element wise to change values from x to -x (using an interface) 我的问题是我希望能够将普通的双精度数组转换为arraylist,我需要对该arraylist元素进行明智的操作,以将x的值更改为-x(使用接口)

I am trying to just get array doubles to convert to an arraylist using a for loop element by element first (thought I should get is working first) but the .add does not seem to work which is the solution that seems to appear when I research it. 我正在尝试仅使用for循环逐个元素地将数组双精度转换为arraylist(以为我应该先工作),但是.add似乎不起作用,这是我研究时似乎出现的解决方案它。 currently it saying "cannot instantiate the type Num" I've tried removing abstract still no good. 目前它说“无法实例化数字类型”我试图删除抽象仍然没有好处。

Am I going about this completely wrong? 我要彻底解决这个问题吗? Thanks for help in advance. 预先感谢您的帮助。

Here are my codes. 这是我的代码。

public interface Num {
    public void neg(); // negation
    public void sqrt(); // square root 
    public String asString(); // number represented as a string
}

public class NumDouble implements Num {

    Double d;
    public NumDouble(Double d) {
        this.d = d;
    }

    @Override
    public void neg() {
        d = -d;
    }

    @Override
    public void sqrt() {
        d = Math.sqrt(d);
    }

    @Override
    public String asString() {
        return d.toString();
    }

}

THIS IS THE CODE I HAVE BEEN TRYING 这是我尝试过的代码

import java.util.ArrayList;
import java.util.Arrays;

public abstract class NumList implements Num {

    ArrayList<Num> nums = new ArrayList<Num>();
    double[] doubles = {2.3, 2.2, 2.4};

    public void neg() {
        for (int i = 0; i < doubles.length; i++) {
            double value = doubles[i];
            nums.add(new Num(value));    <------this is highlighting as an error
        }
        return;
    }

    public void sqrt() {

    }
}

You can either use NumDouble to do so or create an anonymous instance of Num . 您可以使用NumDouble进行创建,也可以创建Num的匿名实例。

The latter would look like something like this: 后者看起来像这样:

Num num = new  Num() {

        Double d;

        @Override
        public void neg() {
            d = -d;
        }

        @Override
        public void sqrt() {
            d = Math.sqrt(d);
        }

        @Override
        public String asString() {
            return d.toString();
        }

    };

As you can see this does not make sense since you can't add constructors to an anonymous instance and your interface does not contain a setter. 如您所见,这是没有意义的,因为您无法将构造函数添加到匿名实例,并且您的接口不包含setter。

Just a note: NumList is not really a Num since it does not fit in the Num abstraction you create. 请注意: NumList并不是真正的Num因为它不适合您创建的Num抽象。

So my suggestion is to either use NumDouble like this: 所以我的建议是像这样使用NumDouble

nums.add(new NumDouble(value));

or create a new class extending Num . 或创建一个扩展Num的新类。

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

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