我需要随机删除java

时间:2016-08-04 03:12:44

标签: java string

好的,我正在研究这个代码,以融合人文学科和STEM。我知道非常基本的java代码,所以我现在正试图坚持使用String方法。我知道使用数组可能更容易,但我在如何使用它们方面没有很好的学习。所以到目前为止,我已经制作了对字符串中的单词进行计数的代码,以确定要删除多少个单词(其中一半)。接下来我需要找出一种方法来随机删除一半的单词并返回一个新的字符串,可能用空格替换删除的字母。

到目前为止,这是我的代码:

public class wordcount
{
    public static void main (String[] args) 
    {

    System.out.println("Simple Java Word Count Program");

    String str1 = "Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting";

    String[] wordArray = str1.split("\\s+");
    int wordCount = wordArray.length;

    System.out.println(str1 + "");

    System.out.println("Word count is = " + wordCount);

    int wordCount2 = wordCount/2;


}

}

2 个答案:

答案 0 :(得分:1)

我将数组复制到arrayList,然后遍历列表并删除随机元素。我希望这是你正在寻找的答案类型。

public static void main(String[] args) {

    String str1 = "Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting";

    String[] wordArray = str1.split("\\s+");
    ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(wordArray));

    int wordCount = wordList.size();
    int halfWordCount = wordCount/2;
    int tracker = 0; //counter for iterations in while loop

    Random random = new Random();
    while(tracker < halfWordCount){
        int randomIndex = random.nextInt(wordList.size());
        wordList.remove(randomIndex);
        tracker++;
    }
    System.out.println(wordList.toString());
}

答案 1 :(得分:0)

import java.util.Arrays;
import java.util.* ;
public class wordcount
{
public ArrayList<Integer> test(Integer[] array) 
    {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i=0; i<array.length; i++)
           list.add(array[i]);
         return list;
    }
public ArrayList<String> testS(String[] array) 
    {
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0; i<array.length; i++)
           list.add(array[i]);
         return list;
    }
public static void main (String[] args) 
{

    System.out.println("Removing random words in a Poem Program");

    String str1 = "Sample Poem by Noah Eli Gordon: Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting";

    String[] wordArray = str1.split("\\s+");
    int wordCount = wordArray.length;

    System.out.println(str1 + "");

    //System.out.println("Word count is = " + wordCount);

    //System.out.println(wordArray);
    //String[] ret = wordArray;
    //for(String str : ret)
    //  System.out.print(str); 

    int wordCount2 = wordCount/2;

    Integer[] myIntArray = new Integer[wordCount];
    //for(int i = 0; i<wordCount;i++)
    //  myIntArray[i] = i;
    //for(int str : myIntArray)
        //System.out.print(str); 

    wordcount w = new wordcount();
    String[] wordArray2 = new String[wordCount2];

    for(int i = 0; i <= wordCount2; i++)
    {
        int rand = (int)(Math.random()*(myIntArray.length-1));
        ArrayList<Integer> list = w.test(myIntArray);
        list.remove(rand);
        myIntArray = list.toArray(myIntArray);
        ArrayList<String> listS = w.testS(wordArray);
        listS.remove(rand);
        wordArray2 = listS.toArray(wordArray);


    }
    List<String> list = new ArrayList<String>();

    for(String s : wordArray2) 
    {
        if(s != null && s.length() > 0) 
        {
            list.add(s);
        }
     }

    wordArray2 = list.toArray(new String[list.size()]);

    //for(int str : myIntArray)
        //System.out.println(str); 
    System.out.println();
    String[] ret2 = wordArray2;
    for(String str : ret2)
        System.out.print(str + " ");
}
}