10.如何在String[] sentences中搜索含有某个String word的所有句子?
import java.util.*;
public class Demo1 {public String[] fun(String[] sentences,String word)
{
//搜索含有某个String word的所有句子
//...?
} public static void main(String[] args) {
String[] sentences={"hello,world","hello,java","wjsflhelloooo","100"};
String word=hello
Demo1 demo1=new Demo1();
String s2=demo1.fun(sentences,word);
//?输出
for(int i=0;i<s2....
}
}
import java.util.*;
public class Demo1 {public String[] fun(String[] sentences,String word)
{
//搜索含有某个String word的所有句子
//...?
} public static void main(String[] args) {
String[] sentences={"hello,world","hello,java","wjsflhelloooo","100"};
String word=hello
Demo1 demo1=new Demo1();
String s2=demo1.fun(sentences,word);
//?输出
for(int i=0;i<s2....
}
}
这句是啥意思?
结果s2要的是什么呢?是数组的元素,还是其他的什么?
{
//搜索含有某个String word的所有句子
//...?
//return null;
} public static void main(String[] args) {
String[] sentences={"hello,world","hello,java","wjsflhelloooo","100"};
String word="hello";
GetWord demo1=new GetWord();
String[] s2=demo1.fun(sentences,word);
//?输出
for(int i=0;i<s2.length;i++)
{
System.out.println(s2[i]);
}
}
}
*
* @author zdjray
*/
import java.util.*;public class Demo1 { public String[] fun(String[] sentences, String word) {
//搜索含有某个String word的所有句子
//...?
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < sentences.length; i++) {
if (sentences[i].indexOf(word) != -1) {
list.add(sentences[i]);
}
} String temp[];
int size = list.size();
if (size > 0) {
temp= new String[size];
for (int i = 0; i < size; i++) {
temp[i] = list.get(i);
}
}
else {
temp = new String[0];
}
return temp;
} public static void main(String[] args) {
String[] sentences = {"hello,world", "hello,java", "wjsflhelloooo", "100"};
String word = "hello";
Demo1 demo1 = new Demo1();
String[] s2 = demo1.fun(sentences, word);
//?输出
for (int i = 0; i < s2.length; i++) {
System.out.println(s2[i]);
}
}
}
另外问一下,jdk1.4.0有开始用ArrayList<String>这样的写法吗?
没有.