1

我有一个名为 data 的 ArrayList。

data 中的每个索引都包含一行单词和数字。例如:

data(0) contains: Bob Sinclair, BS776, 87.5, 23.4, 20.1, 50.2

data(1) contains: Paul Paulson, PP009, 98.3, 12.6, 34.0, 22.5

...再加上 8 个

有没有办法可以将每个对象分成 6 个对象(姓名、学生编号、分数 1、分数 2、分数 3、分数 4)?

之后我需要将分数加在一起以获得总分,但我并没有寻求这部分的帮助,只是提到它是因为数字不能是字符串,因为它们需要添加(或者它们可以是字符串,/耸耸肩,我是菜鸟)。

4

8 回答 8

2

是的,给猫剥皮的方法不止一种:

第一:String.split()

String[] parts = inputString.split(','); //split the input
String name = parts[0];                  //get first part. 
double[] scores = new double[4];         //create array for doubles 
for(int i=0; i<4; i++) {                 //fill array of doubles --> array index checking might be needed!
    scores[i]=Double.parseDouble(scores[i+2]);
}

但是,由于这涉及浮点数,您应该阅读每个计算机科学家都应该知道的关于浮点运算的知识,了解为什么他们可以显示有趣的结果......或者,您可以使用 BigDecimals

BigDecimal exactResult = BigDecimal.valueOf(scores[i+2]);

第二种可能:扫描仪

Scanner s = new Scanner(inputString);
s.useDelimiter(",");
String name = s.next();
double[] scores = new double[4];         //create array for doubles 
String someCode = s.next();
for(int i=0; i<4; i++) {                 //fill array of doubles 
    scores[i]=Double.parseDouble(s.next()); //null check might be needed!
}

第三:使用模式

//this pattern is very ugly (unsacalable, unmaintainable), but should work for now as poc...
Pattern p = Pattern.compile("([^,]*),\\s*([^,]*),\\s*([^,]*),\\s*([^,]*),\\s*([^,]*),\\s*([^,]*)");
Matcher m = p.matcher(inputString);
if(matcher.matches()) {
   String name = group(1);
   String someCode = group(2);
   double[] scores = new double[4];         //create array for doubles 
   for(int i=0; i<4; i++) {                 //fill array of doubles 
      scores[i]=Double.parseDouble(s.group(i+3)); //null check might be needed!
   }
}
于 2013-09-30T12:02:25.470 回答
0
List<String> list = new ArrayList<String>();
list.add("Bob Sinclair, BS776, 87.5, 23.4, 20.1, 50.2");
list.add("Paul Paulson, PP009, 98.3, 12.6, 34.0, 22.5");
for (String str : list) {
String[] parts = str.split(", ");
// create structure which you need and put data there
// parts[0] - student, etc.
}
于 2013-09-30T12:00:06.377 回答
0

您需要将数据包装到一个类中,然后将该类的实例存储在您的ArrayList.

假设你有一堂课Player

public class Player {

    private String name;
    private List<Float> scores;

    public Player(String name){

    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Float> getScores() {
        return scores;
    }
    public void setScores(List<Float> scores) {
        this.scores = scores;
    }
}

然后,您将 ArrayList 创建为List<Player> players = new ArrayList<Player>.

使用 Player 对象的 getter 和 setter,您可以检索每个单独的属性并按照您的喜好使用它们。

因此,要仅获取该实例的名称,您可以: players.get(0).getName()或对存储在播放器类中的任何变量执行相同操作。

于 2013-09-30T12:01:38.380 回答
0

它们可以是字符串,然后可以使用字符串拆分方法将它们分成 6 个字符串,并使用 Float.parseFloat("此处为字符串格式的数字")。

类 API 链接:http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#parseFloat(java.lang.String)

于 2013-09-30T11:58:59.343 回答
0

这就是你所做的Object Oriented Programming

即创建一个Student具有属性的类Name, Student Number, Score1, Score2, Score3, Score4

class Student {
  String name;
  int num;
  int score1;
  int score2;
  int score3;
  int score4;
  // getters setters here
}

并从你的每个元素填充这个Student类的实例ArrayList

List<Student> slist = new ArrayList<Student>();
for (String str : list) {
    String[] arr = str.split(",");
    // some validation here for generated arr
    student student = new Student();
    student.setName(arr[0]);
    student.setName(Integer.parseInt(arr[1]));
    // populate rest of fields
    slist.add(student);
}
System.out.println(slist);
于 2013-09-30T11:59:06.310 回答
0

像这样

for (String str : data) {

      //use split with , on each string here

            }
于 2013-09-30T11:59:37.993 回答
0

你可以创建一个类,它得到一个数据对象,我想它是一个字符串,作为你的构造函数的参数。

如果它是一个字符串,你可以通过string.split(",");

通过您的课程,您可以轻松地创建方法来获得所有想要的值。

于 2013-09-30T11:59:52.313 回答
0

使用这样split的类中的方法String,假设它data具有List<String>类型并且每一行都具有您描述的结构:

for(String s: data) {
    String[] line = s.split(",");
    // use each element in line
    String studentName = line[0];
    String studentNumber = line[1];
    double score1 = Double.parseDouble(line[2]);
    double score2 = Double.parseDouble(line[3]);
    double score3 = Double.parseDouble(line[4]);
    double score4 = Double.parseDouble(line[5]);
}
于 2013-09-30T12:04:07.183 回答