gpt4 book ai didi

java - StringTokenizer在JAVA中拆分字符串效率更高吗?

转载 作者:行者123 更新时间:2023-11-30 06:14:09 24 4
gpt4 key购买 nike

我一直在解决一个问题Anti-Blot System来自 SPOJ

首先,我尝试使用 String 的 split 方法拆分输入字符串,提交后我得到了 TLE

我的代码使用split方法

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


class ABSYS {
public static void main(String[] args) throws IOException {
int t;
String[] numArray = new String[2];
String[] numArray2 = new String[2];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(reader.readLine());
while(t > 0) {
String input = reader.readLine();
if(input.isEmpty()) {
continue;
}
numArray = input.split("\\s{1}=\\s{1}");
numArray2 = numArray[0].split("\\s{1}\\+\\s{1}");
Pattern pattern = Pattern.compile("machula");
Matcher matcher = pattern.matcher(numArray[1]);
if(matcher.find()) {
System.out.println(numArray[0] + " = " + (Integer.parseInt(numArray2[0]) + Integer.parseInt(numArray2[1])));
}
else {
matcher = pattern.matcher(numArray2[0]);
if(matcher.find()) {
System.out.println((Integer.parseInt(numArray[1]) - Integer.parseInt(numArray2[1])) + " + " + numArray2[1] + " = " + numArray[1]);
}
else {
System.out.println(numArray2[0] + " + " + (Integer.parseInt(numArray[1]) - Integer.parseInt(numArray2[0])) + " = " + numArray[1]);
}
}
t--;
}
}
}

经过多次尝试我都失败了,无法让我的代码更省时

然后,今天我阅读了有关 StringTokenizer 的内容,并在我的代码中使用了它,然后我就在那里(在 spoj 上)

我的代码使用 StringTokenizer

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.StringTokenizer;


class ABSYS {
public static void main(String[] args) throws IOException {
int t, a = 0, b = 0, c = 0, matchula = 0;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Pattern pattern = Pattern.compile("^(\\d)+$");
t = Integer.parseInt(reader.readLine());
while(t > 0) {
String input = reader.readLine();
if(input.isEmpty()) {
continue;
}
StringTokenizer tokenizer = new StringTokenizer(input);
String token = tokenizer.nextToken();
if(pattern.matcher(token).matches()) {
a = Integer.parseInt(token);
}
else
matchula = 1;

tokenizer.nextToken();
token = tokenizer.nextToken();
if(pattern.matcher(token).matches()) {
System.out.println("b = " + token);
b = Integer.parseInt(token);
}
else
matchula = 2;

tokenizer.nextToken();
token = tokenizer.nextToken();
if(pattern.matcher(token).matches()) {
c = Integer.parseInt(token);
}
else
matchula = 3;
switch(matchula) {
case 1: System.out.println((c-b) + " + " + b + " = " + c);
break;
case 2: System.out.println(a + " + " + (c-a) + " = " + c);
break;
case 3: System.out.println(a + " + " + b + " = " + (a+b));
break;
}
t--;
}
}
}

在 JAVA Docs 中,他们不鼓励使用 StringTokenizer。

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Jason S 中所述回答here

if I wanted to tokenize a string with more complex logic than single characters (e.g. split on \r\n), I can't use StringTokenizer but I can use String.split().

我的疑问

  1. 为什么会这样,尽管我发现它更省时。
  2. 不鼓励使用 StringTokenizer 的原因是什么?
  3. 如果像我的问题一样想使用简单的正则表达式,那么 StringTokenizer 是否比 String.split() 更好?

最佳答案

String.split() 比 StringTokenizer 更灵活、更易于使用。 StringTokenizer 早于 Java 对正则表达式的支持,而 String.split() 支持正则表达式,这使得它比 StringTokenizer 强大得多。 String.split 的结果也是一个字符串数组,这通常是我们想要的结果。 StringTokenizer 确实比 String.split() 更快,但对于大多数实际用途而言,String.split() 已经足够快了。

查看此问题的答案以获取更多详细信息 Scanner vs. StringTokenizer vs. String.Split

关于java - StringTokenizer在JAVA中拆分字符串效率更高吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30781208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com