ArrayList 的输入

发布于 2024-09-24 14:05:57 字数 2721 浏览 2 评论 0原文

我在将输入字符串格式化为 ArrayList 时遇到问题。我评论了一些我尝试过的事情。最初我尝试将输入放入字符串中,然后添加到 arrayList 中。

输入是一长串:

(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500), (A,Name3,800), (A,Name4,150), (A,Name5,850), (A,Name6,750), (A,Name7,950), (A,Name8,250), (R,Name6,750), (A,Name10,450), (A,Name11,1000)*emphasized text*

到目前为止我所拥有的:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author Joshua
 */
public class HighScore {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
/**


    Scanner sc = new Scanner(System.in);
    String scores = sc.nextLine();

    scores = scores.replace('(', '"');
    scores = scores.replace('(', '"');

    StringBuffer str = new StringBuffer(scores);

   for (int i = 0; i < scores.length(); i++)

   {
    if (scores.charAt(i) == ',')

    {  
       str.insert(i, 'H');
    }
   }
      System.out.println(str);

*/
      ArrayList<Score> list = new ArrayList<Score>();
      ArrayList <Score> topscore = new ArrayList<Score>();

/**      
      list.add (new Score("A","1111",99999));
      list.add (new Score("A","2222",88888));
      list.add (new Score("A","3333",77777));
      list.add (new Score("A","4444",66666));
      list.add (new Score("A","5555",55555));
      list.add (new Score("A","6666",44444));
      list.add (new Score("R","4444",66666));
      list.add (new Score("A","7777",22222));
      list.add (new Score("A","8888",11111));

*/
      Collections.sort(list);



//For loop to add all the high scores with 'A' as the command to the ArrayList topscore

       for(Score addScore : list)
       {
           if (addScore.getCommand().equalsIgnoreCase("A"))
           topscore.add(addScore);
       }

//For loop to remove all the high scores with 'R' as the command from the ArrayList topscore

       for(Score remScore : list)
       {
           if (remScore.getCommand().equalsIgnoreCase("R"))
                  for (int i = 0; i < topscore.size(); i++)
                  {
                   if (remScore.getName().equals(topscore.get(i).getName()))
                       if(remScore.getScoreValue() == topscore.get(i).getScoreValue())
                            topscore.remove(i);
                  }
       }


//Prints the finished finalScore list

      for(Score finalScore : topscore)
       {
        System.out.println(finalScore);
       }





    /**

    String s[]=new String[100];
    for(int i=0;i<s.length;i++)
        {
        s = scores.substring(1, scores.length()-1).split("\\), \\(");
        }

*/

 sc.close();
 }
}

I'm having issues formatting an input String into an ArrayList. I commented out some of the things I've tried. Initially I tried to put the input into a string then add to arrayList.

The input is one long string:

(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500), (A,Name3,800), (A,Name4,150), (A,Name5,850), (A,Name6,750), (A,Name7,950), (A,Name8,250), (R,Name6,750), (A,Name10,450), (A,Name11,1000)*emphasized text*

What I have so far:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author Joshua
 */
public class HighScore {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
/**


    Scanner sc = new Scanner(System.in);
    String scores = sc.nextLine();

    scores = scores.replace('(', '"');
    scores = scores.replace('(', '"');

    StringBuffer str = new StringBuffer(scores);

   for (int i = 0; i < scores.length(); i++)

   {
    if (scores.charAt(i) == ',')

    {  
       str.insert(i, 'H');
    }
   }
      System.out.println(str);

*/
      ArrayList<Score> list = new ArrayList<Score>();
      ArrayList <Score> topscore = new ArrayList<Score>();

/**      
      list.add (new Score("A","1111",99999));
      list.add (new Score("A","2222",88888));
      list.add (new Score("A","3333",77777));
      list.add (new Score("A","4444",66666));
      list.add (new Score("A","5555",55555));
      list.add (new Score("A","6666",44444));
      list.add (new Score("R","4444",66666));
      list.add (new Score("A","7777",22222));
      list.add (new Score("A","8888",11111));

*/
      Collections.sort(list);



//For loop to add all the high scores with 'A' as the command to the ArrayList topscore

       for(Score addScore : list)
       {
           if (addScore.getCommand().equalsIgnoreCase("A"))
           topscore.add(addScore);
       }

//For loop to remove all the high scores with 'R' as the command from the ArrayList topscore

       for(Score remScore : list)
       {
           if (remScore.getCommand().equalsIgnoreCase("R"))
                  for (int i = 0; i < topscore.size(); i++)
                  {
                   if (remScore.getName().equals(topscore.get(i).getName()))
                       if(remScore.getScoreValue() == topscore.get(i).getScoreValue())
                            topscore.remove(i);
                  }
       }


//Prints the finished finalScore list

      for(Score finalScore : topscore)
       {
        System.out.println(finalScore);
       }





    /**

    String s[]=new String[100];
    for(int i=0;i<s.length;i++)
        {
        s = scores.substring(1, scores.length()-1).split("\\), \\(");
        }

*/

 sc.close();
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-10-01 14:05:57

您还可以使用正则表达式和捕获组来完成此操作:

public static void main(String[] args) {

    String input = "(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500)";
    Pattern p = Pattern.compile("\\(([A,R]?),(\\w+?),(\\d+?)\\)");
    Matcher m = p.matcher(input);

    ArrayList<Score> scores = new ArrayList<Score>();
    ArrayList<Score> topScores = new ArrayList<Score>();

    while (m.find()) {
        String action = m.group(1);
        String name = m.group(2);
        double scoreVal = Double.valueOf(m.group(3));

        Score score = new Score(name, scoreVal);
        scores.add(score);

        if ("A".equalsIgnoreCase(action)) {
            topScores.add(score);
        } else { // remove
            for (Score topScore : topScores) {
                // make sure you have implemented equals() in the Score class
                if (topScore.equals(score)) {
                    topScores.remove(score);
                }
            }
        }
    }

    // Prints the finished finalScore list
    for (Score finalScore : topScores) {
        System.out.println(finalScore);
    }
}

You can also do it using a regex and capturing groups:

public static void main(String[] args) {

    String input = "(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500)";
    Pattern p = Pattern.compile("\\(([A,R]?),(\\w+?),(\\d+?)\\)");
    Matcher m = p.matcher(input);

    ArrayList<Score> scores = new ArrayList<Score>();
    ArrayList<Score> topScores = new ArrayList<Score>();

    while (m.find()) {
        String action = m.group(1);
        String name = m.group(2);
        double scoreVal = Double.valueOf(m.group(3));

        Score score = new Score(name, scoreVal);
        scores.add(score);

        if ("A".equalsIgnoreCase(action)) {
            topScores.add(score);
        } else { // remove
            for (Score topScore : topScores) {
                // make sure you have implemented equals() in the Score class
                if (topScore.equals(score)) {
                    topScores.remove(score);
                }
            }
        }
    }

    // Prints the finished finalScore list
    for (Score finalScore : topScores) {
        System.out.println(finalScore);
    }
}
○闲身 2024-10-01 14:05:57

将此用作伪代码。

public static void main(String[] args) {
    String input = "(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500)";
    String[] scoreSets = input.split("\\),");
    List<Score> scoreList = new ArrayList<Score>();
    for (String score : scoreSets) {
        score = score.trim().substring(1);
        int closeBrace = score.indexOf(')');
        if (closeBrace == score.length() - 1) {
            score = score.substring(0, score.length() - 1);
        }
        String[] tokens = score.split(",");
        scoreList.add(new Score(tokens[0],tokens[1],Double.valueOf(tokens[2])));
    }

}

Use this as a pseudo code.

public static void main(String[] args) {
    String input = "(A,Name1,200), (A,Name1,200), (R,Name1,200), (A,Name2,900), (A,Name2,500)";
    String[] scoreSets = input.split("\\),");
    List<Score> scoreList = new ArrayList<Score>();
    for (String score : scoreSets) {
        score = score.trim().substring(1);
        int closeBrace = score.indexOf(')');
        if (closeBrace == score.length() - 1) {
            score = score.substring(0, score.length() - 1);
        }
        String[] tokens = score.split(",");
        scoreList.add(new Score(tokens[0],tokens[1],Double.valueOf(tokens[2])));
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文