简体   繁体   English

如何在跑步者中将此数字列表设置为构造函数中的ArrayList?

[英]How do I set this list of numbers in my runner to an ArrayList in the constructor?

I've been struggling with this for a while now. 我已经为此苦了一段时间了。 I know that I can instantiate a String to an ArrayList in the constructo but I don't know how I would instantiate a group of ints to a ArrayList from the runner without doing it all manully using list.add(number) over and over again. 我知道我可以在构造函数中将String实例化为ArrayList,但是我不知道如何从跑步者实例化一组int到ArrayList,而无需一遍又一遍地使用list.add(number)手动完成所有操作。 。

void setup() { //My runner
  NumAnalyzer test = new NumAnalyzer("5 12 9 6 1 4 8 6");

  out.println("even count = "+test.countEvens());
  out.println("odd count = "+test.countOdds());
  out.println("perfect count = "+test.countPerfects()+"\n\n\n");


  //add more test cases
}




import java.util.ArrayList;    
import java.util.Scanner;
import static java.lang.System.*;

public class NumAnalyzer{
  private ArrayList<Number> list;

  public NumAnalyzer(String word){
    list = new ArrayList<Number>(); //This instantiates it as a String but I want it to instantiate it as a list of integers. 
  }


  public int countOdds(){ //counting odds
      int oddCount=0;
      for(int x=0; x<list.size(); x++){
        if(list.get(x)%2 != 0){
          oddCount++;
        }
      }
      return oddCount;
  }

  public int countEvens(){ //counting evens
      int evenCount=0;
     for(Number num : list){
        if(list.get(num)%2 == 0){
           evenCount++;
        }
      }
      return evenCount;
  }

  public int countPerfects() {
     int perfectCount=0;
     for(int x=0; x<list.size(); x++){

       int mult=2, factorsum=0; //gets the sum of the multiples
       while(list.get(x) != mult){
         if(list.get(x)%mult == 0){
           factorsum=factorsum+list.get(x);
         }
         mult++;
       }

       if(factorsum == list.get(x)){ //testing to see if it's perfect
         perfectCount++;
       }
     }
     return perfectCount;
  }

  public String toString( ){





    return "Number of Odds:"+oddCount+"/n Number of Evens:"+evenCount+"/n Number of Perfects:"+perfectCount+list;
  }
}

Overall I just want the constructor to instantiate the ArrayList with the numbers from the runner. 总的来说,我只希望构造函数使用流道中的数字实例化ArrayList。

In your constructor, you can split the word using spaces and then convert each token to an integer and finally collect to a list:- 在构造函数中,您可以使用空格对单词进行分割,然后将每个标记转换为整数,最后收集到列表中:

list = Arrays.stream(word.split("\\s+"))
        .map(Integer::parseInt)
        .collect(Collectors.toList());

Update 更新

For numbers instead of just integers, you can use this mapping:- 对于数字而不是整数,可以使用此映射:

.map(s -> s.contains(".") ? (Number) Double.parseDouble(s) : Integer.parseInt(s))

If you strictly want ArrayList as opposed to any list, you can collect it as:- 如果您严格要求ArrayList而不是任何列表,则可以将其收集为:

.collect(Collectors.toCollection(ArrayList::new));

But it's better to use varargs if you are not working with Strings. 但是,如果您不使用字符串,最好使用varargs。

Why do you need a constructor with String if you're not willing to use it? 如果不愿意使用String为什么需要构造函数?

Varargs constructor will fit here well: Varargs构造函数非常适合这里:

class NumAnalyzer {
    private List<Number> list;

    public NumAnalyzer(Number... numbers) {
        list = Arrays.asList(numbers);
    }
}

void setup() {
    NumAnalyzer test = new NumAnalyzer(5, 12, 9, 6, 1, 4, 8, 6);
}

Pay attention that it's much better design desicition to use private List<Number> list instead of private ArrayList<Number> list . 请注意,使用private List<Number> list而不是private ArrayList<Number> list是更好的设计目标。

Moreover, this code will not compile because you're treating Numbers as Integers . 而且,由于您将Numbers视为Integers ,因此该代码将无法编译。 If you're really expecting any type of number, passing them as a String will require more granular parsing. 如果您确实希望使用任何类型的数字,则将它们作为String传递将需要更精细的解析。

  1. You can remove all white spaces using replaceAll method and regexp or „ „. 您可以使用replaceAll方法和regexp或„„删除所有空格。
  2. Then, you can iterate over string chars and try to parse them into number. 然后,您可以遍历字符串char并尝试将其解析为number。
  3. After that, just add it to list of numbers. 之后,只需将其添加到数字列表中即可。

However, You need to consider what happen if number has more than 1 digit 但是,您需要考虑如果数字多于1位会发生什么情况

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

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