Java 确定数组列表是否开始和结束,并显示其间的所有内容
我正在尝试筛选一个数组列表,该列表包含存储在以下位置的URL的内容:Java 确定数组列表是否开始和结束,并显示其间的所有内容,java,arrays,Java,Arrays,我正在尝试筛选一个数组列表,该列表包含存储在以下位置的URL的内容:(list quotes=new ArrayList();),并显示标记之间的所有内容的结果(所有引号都放在这两个标记之间)。我已经了解了打印部分,但是java中是否有任何方法允许您按照我指定的方式过滤数组列表?谢谢 List<String> cookies = new ArrayList<>(); public void init() throws ServletException {
(list quotes=new ArrayList();)
,并显示
标记之间的所有内容的结果(所有引号都放在这两个标记之间)。我已经了解了打印部分,但是java中是否有任何方法允许您按照我指定的方式过滤数组列表?谢谢
List<String> cookies = new ArrayList<>();
public void init() throws ServletException
{
try
{
URL url = new URL(" http://fortunes.cat-v.org/openbsd/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line ;
while((line = in.readLine()) != null)
{
cookies.add(line);
//line = in.readLine();
}
in.close();
}
catch (java.net.MalformedURLException e)
{
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e)
{
System.out.println("I/O Error: " + e.getMessage());
}
}
int startIndex=quotes.IndexOf("<pre>");
int endIndex=c.IndexOf("</pre>");
for(int i=startIndex ; i<=endIndex ; i++){
// do something here ...
// System.out.println(quotes.get(i));
}
更多详情:
因此,您有一个普通的html文件,其中包含各种标记。假设我扫描页面并将所有文本存储在字符串数组中。我只想显示
标记之间的内容,不想显示其余内容。希望这有帮助
List<String> cookies = new ArrayList<>();
public void init() throws ServletException
{
try
{
URL url = new URL(" http://fortunes.cat-v.org/openbsd/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line ;
while((line = in.readLine()) != null)
{
cookies.add(line);
//line = in.readLine();
}
in.close();
}
catch (java.net.MalformedURLException e)
{
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e)
{
System.out.println("I/O Error: " + e.getMessage());
}
}
int startIndex=quotes.IndexOf("<pre>");
int endIndex=c.IndexOf("</pre>");
for(int i=startIndex ; i<=endIndex ; i++){
// do something here ...
// System.out.println(quotes.get(i));
}
以下是文本的存储方式:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String [] args){
//This list is supposed filled with some values
List<String> quotes = new ArrayList<String>();
for(String quote:quotes){
Pattern pattern = Pattern.compile(".*?<pre>(.*?)</pre>.*?");
Matcher m = pattern.matcher(quote);
while(m.find()){
String result = m.group(1);
System.out.println(result);
}
}
}
List cookies=new ArrayList();
public void init()引发ServletException
{
尝试
{
URL=新URL(“http://fortunes.cat-v.org/openbsd/");
BufferedReader in=新的BufferedReader(新的InputStreamReader(url.openStream());
弦线;
而((line=in.readLine())!=null)
{
cookies.add(行);
//line=in.readLine();
}
in.close();
}
catch(java.net.MalformedURLException)
{
System.out.println(“格式错误的URL:+e.getMessage());
}
捕获(IOE异常)
{
System.out.println(“I/O错误:+e.getMessage());
}
}
您可以找到字符串“pre”的索引和“/pre”的索引,并为它们之间的所有元素循环
intstartindex=quotes.IndexOf(“”);
int endIndex=c.IndexOf(“”);
对于(inti=startIndex;i使用正则表达式,下面是一个完整的工作示例
import java.util.ArrayList;
导入java.util.List;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公开课考试{
公共静态void main(字符串[]args){
//这个列表应该包含一些值
列表引号=新的ArrayList();
for(字符串引号:引号){
Pattern=Pattern.compile(“.*?(.*?。”);
匹配器m=模式匹配器(引号);
while(m.find()){
字符串结果=m.group(1);
系统输出打印项次(结果);
}
}
}
}至少对我来说,这个问题有点不清楚。你能提供一个例子说明这个列表可能包含什么以及你想要打印什么吗?所以你有一个普通的html文件,其中包含各种各样的标记。比如说我扫描页面并将所有文本存储在字符串数组中。我只想显示标记之间的内容,而不想显示其他内容。希望这个帮助psIt并没有真正的帮助。你需要告诉我们html是如何存储在ArrayList
中的。它可以是逐行存储的,也可以是长度为1的String
s或其他内容。也可以在问题中包含这些信息,不要只是发表评论,因为在那里更容易找到,并且不是所有用户都愿意阅读所有的评论。我添加了一些代码,希望这能澄清问题,谢谢你的回答。我如何指定引号是否存储在%符号之间?例如:%quote 1%quote 2%quote 3%quote 4%quote 5%quote…%;百分比位于如下标记之间:%quote 1%quote 2%quote 3%quote 4%quote 5%quote…%