ArrayList和Passing值

时间:2016-02-25 23:29:39

标签: java eclipse arraylist jcreator

我是编程的新手,我们今天刚刚在课堂上学习了ArrayLists,对于你们我有一个简单的问题,我似乎无法在关于如何设置传递值等于的注释中找到它。这个练习程序的要点是接受一个Number对象(该类已经被创建),并且ArrayList中的那些数字应该被计为赔率,均衡和完美数字。这是程序的前几行,这就是你应该需要的所有内容。

import java.util.ArrayList;
import static java.lang.System.*;
public class NumberAnalyzer {

    private ArrayList<Number> list;

    public NumberAnalyzer() {
        list = new ArrayList<Number>();
    }

    public NumberAnalyzer(String numbers) {

    }

    public void setList(String numbers) {

    }

我应该在NumberAnalyzer()和setList()中设置什么(String number)?在此先感谢您回答菜鸟问题!

    NumberAnalyzer test = new NumberAnalyzer("5 12 9 6 1 4 8 6");
    out.println(test);
    out.println("odd count = "+test.countOdds());
    out.println("even count = "+test.countEvens());
    out.println("perfect count = "+test.countPerfects()+"\n\n\n");

这是将运行该程序的Lab16b类。 ^^

public class Number
{
private Integer number;
public Number()
{
    number = 0;
}
public Number(int num)
{
    number = num;
}
public void setNumber(int num)
{
    number = num;
}
public int getNumber()
{
    return 0;
}
public boolean isOdd()
{
    return number % 2 != 0;
}
public boolean isPerfect()
{
    int total=0;
    for(int i = 1; i < number; i++)
    {
        if(number % i == 0)
        {
            total = total + i;
        }
    }
    if(total == number)
    {
        return true;
    }
    else
    {
        return false;
    }
}
public String toString( )
{
    return "";
}
}

这是Number类。 ^^

2 个答案:

答案 0 :(得分:1)

根据您提供的信息,我觉得NumberAnalyzer应该是这样的。 setList函数目前用于获取String并将其中的数字添加到新列表中。

public class NumberAnalyzer {

    private List<Number> list;

    public NumberAnalyzer() {
        this.list = new ArrayList<Number>();
    }

    public NumberAnalyzer(String numbers) {
        setList(numbers);
    }

    public void setList(String numbers) {
        String[] nums = numbers.split(" ");
        this.list = new ArrayList<Number>();
        for(String num: nums) 
            list.add(new Number(Integer.parseInt(num)));
    }

}

答案 1 :(得分:0)

分析以学习一些东西。

public static void main(String[] args) {

    String temp = "5 12 9 6 1 4 8 6";
    NumberAnalyzer analyzer = new NumberAnalyzer(temp);

    //foreach without lambda expressions
    System.out.println("without Lambda");
    for (NeverNumber i : analyzer.getList()) {
        i.print();
    }
    //with the use of lambda expressions, which was introduced in Java 8
    System.out.println("\nwith Lambda");
    analyzer.getList().stream().forEach((noNumber) -> noNumber.print());

    NeverNumber number = new NeverNumber(31);
    number.print();
    number.setNumber(1234);
    number.print();

}

public class NumberAnalyzer {

    private List<NeverNumber> list; //List is interface for ArrayList

    public NumberAnalyzer(String numbers) {
        String[] numb=numbers.split(" ");
        this.list=new ArrayList<>();
        for (String i : numb) {
            list.add(new NeverNumber(Integer.parseInt(i)));
        }
    }

    public void setList(List<NeverNumber> numbers) {
        List<NeverNumber> copy=new ArrayList<>();
        numbers.stream().forEach((i) -> {
            copy.add(i.copy());
        });
        this.list=copy;
    }

    public List<NeverNumber> getList() {
        List<NeverNumber> copy=new ArrayList<>();
        this.list.stream().forEach((i) -> {
            copy.add(i.copy());
        });
        return copy;
    }

    public NeverNumber getNumber(int index) {
        return list.get(index).copy();
    }

}

public class NeverNumber { //We do not use the names used in the standard library.
    //In the library there is a class Number.

    private int number; // If you can use simple types int instead of Integer.

    public NeverNumber() {
        number = 0;
    }

    public NeverNumber(int num) {
        number = num;
    }

    private NeverNumber(NeverNumber nn) {
        this.number=nn.number;
    }

    public void setNumber(int num) {
        number = num;
    }

    public int getNumber() {
        return this.number;
    }

    public boolean isOdd() {
        return number % 2 != 0;
    }

    public boolean isPerfect() {
        long end = Math.round(Math.sqrt(number)); //Method Math.sqrt(Number) returns a double, a method Math.round(double) returns long.
        for (int i = 2; i < end + 1; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    public NeverNumber copy(){
        return new NeverNumber(this);
    }

    public void print() {
        System.out.println("for: " + this.toString() + " isPer: " + this.isPerfect() + " isOdd: " + this.isOdd() + "\n");
    }

    @Override //Every class in Java inherits from the Object class in which it is toString(), 
    //so we have to override our implementation.
    public String toString() {
        return this.number + "";  //The object of any class + "" creates a new object of the String class, 
        //that is for complex types, calls the toString () method implemented in this class, 
        //override the toString () from the Object class. If the runs, we miss class toString() 
        //calls from the Object class.
    }

}