Home > Mobile >  Split a string in list array
Split a string in list array

Time:11-05

How can I split each of the sentence in the arraylist? what i'm trying to make is, a different set of sentence to be splitted and shuffle, and student will arrange it in correct order

public class QuestionActivity1 extends AppCompatActivity {

    private int presCounter = 0;
    private int maxpresCounter;

    private List<QuestionModel1> list;

    TextView textScreen, textQuestion, textTitle;
    Button submitBtn;


    @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question1);
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        submitBtn=(Button) findViewById(R.id.submitBtn);


        list=new ArrayList<>();
        list.add(new QuestionModel1("The kids are playing"));
        list.add(new QuestionModel1("The kids are sleeping"));
        list.add(new QuestionModel1("The kids are dancing"));
        

        keys=shuffleArray(keys);
        for (String key : keys){
            addView(((LinearLayout) findViewById(R.id.layoutParent)), key, ((EditText) findViewById(R.id.editText)));
        }
        maxpresCounter= keys.length;

    }

I can only try one

private String sentence="The kids are playing";
private String[] keys=sentence.split(" ");

QuestionModel Class

public class QuestionModel1 {
    private String sentence;

    public QuestionModel1(String sentence) {
        this.sentence = sentence;
    }

    public String getSentence() {
        return sentence;
    }

    public void setSentence(String sentence) {
        this.sentence = sentence;
    }
}

CodePudding user response:

    for (QuestionModel1 questionModel1 : list) {
        String sentence = questionModel1.getSentence();
        String[] keys = sentence.split(" ");
        // do what you need below for each key.
        // shuffleArray(keys); <--- A stab at what you need ??? 
    }

Something like this?

CodePudding user response:

So, assuming we have the following array list:

List<QuestionModel1> list = new ArrayList();
list.add(new QuestionModel1("The kids are playing"));
list.add(new QuestionModel1("The kids are sleeping"));
list.add(new QuestionModel1("The kids are dancing"));

you could split it with the following snippet, assuming you're using at least Java 8

List<List<String>> newList = list.stream()
                .map(item -> Arrays.asList(item.getSentence().split(" ")))
                .collect(Collectors.toList());

and this would yield the following List<List<String>> as a result

[[The, kids, are, playing],[The, kids, are, sleeping],[The, kids, are, dancing]]

Now, if you just want the words in one long list (including duplicates), you could just add one additional .flatMap(...) operation

List<String> keys = list.stream()
                .map(item -> Arrays.asList(item.getSentence().split(" ")))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

which would yield the following List<String> as a result:

[The, kids, are, playing, The, kids, are, sleeping, The, kids, are, dancing]

This isn't the only approach (you could use .reduce(...) instead of .flatMap(...), for example), but it should fit your problem.

  • Related