到目前为止,我见过的所有样本函数都会因某种原因避免返回一个字符串.就Java来说,我是一个新秀,所以我不确定这是否是故意的.我知道在C++中,返回对字符串的引用比返回该字符串的副本更有效.
这在Java中如何工作?
我对Java for Android特别感兴趣,其资源比桌面/服务器环境更有限.
为了帮助这个问题更集中,我提供了一个代码片段,我有兴趣返回(给调用者)字符串页面:
public class TestHttpGet {
private static final String TAG = "TestHttpGet";
public void executeHttpGet() throws Exception {
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.google.com/"));
HttpResponse response = client.execute(request); // actual HTTP request
// read entire response into a string object
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = …Run Code Online (Sandbox Code Playgroud) 以下代码片段提取一个且仅一个元素,特别是第一个元素:
String linkHref = "";
String linkText = "";
Elements links = div.getElementsByTag("a");
for (Element link : links) {
linkHref = link.attr("href");
linkText += link.text();
break;
}
Run Code Online (Sandbox Code Playgroud)
与简洁相比,这是非常繁琐的代码,links.get(0)但它有一个重要特性:如果Elements为空,它将不会抛出IndexOutOfBoundException.相反,它只会将字符串留空.
我可以将它封装到我自己的函数中,但我很难相信Jsoup已经没有这样的功能了(我更喜欢使用库函数而不是"尽可能地重新发明轮子").我搜索了文档但找不到任何文档.
你知道Elements.get(0)Jsoup中是否存在这样的"安全"吗?
我试图在Activity类中设置一个断点:
public class MainActivity extends Activity {
private EditText htmlContent = null;
private Button getBtn = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // <<<< this is where I set a breakpoint
htmlContent = (EditText)findViewById(R.id.htmlContent);
getBtn = (Button)findViewById(R.id.get);
// anonymous inner class implementing OnClickListener
getBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// fill htmlContent using HTTP GET
try {
final TestHttpGet thgObj = new TestHttpGet();
thgObj.executeHttpGet();
} …Run Code Online (Sandbox Code Playgroud) 在下面的简短代码片段中,Eclipse标记了有关语句String key = pair.getKey();和String value = pair.getValue();语句的错误,并说明了这一点
"类型不匹配:无法从Object转换为String"
这是相关代码:
for (Map<String, String> dic : dics) {
Iterator it = dic.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String key = pair.getKey();
String value = pair.getValue();
}
}
Run Code Online (Sandbox Code Playgroud)
这是为什么?
到目前为止我看到的所有示例都没有将pair.getKey()或pair.getValue()强制转换为String,所以我想在继续解决之前了解发生了什么.
我想声明一个超类私有的数据成员:
public abstract class superclass {
private int verySensitive;
abstract int setVerySensitive(int val); // must be overriden by subclass to work properly
}
public class subclass extends superclass {
@Override
protected int setVerySensitive(int val) {
if (val > threshLow && val < threshHigh) // threshHigh is calculated in superclass constructor
verySensitive = val;
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我在这里有一个问题:超类不能访问verySensitive,因为它是私有的,但我不想让verySensitive受到保护,因为它是......敏感的.
另请注意,setVerySensitive是抽象的,因为只有在构造了超类之后才能检查有效值.
你能推荐一种优雅的方式摆脱这种"捕获22"的局面吗?
假设目标字符串一方面是任意的,但另一方面保证包含一个十进制数字(1位或更多位数),我提出了以下常规正则表达式模式:
.*?(\d+).*?
Run Code Online (Sandbox Code Playgroud)
因此,如果目标字符串是"(这是数字200)",例如,Matcher.group(1)将包含该数字.
是否有更优化的正则表达式模式(或非正则表达式方法)来提取此数字?
"最佳"我的意思是最快(可能具有最少的CPU周期).仅限Java.
java ×4
android ×1
debugging ×1
html ×1
html-parsing ×1
jsoup ×1
map ×1
oop ×1
optimization ×1
performance ×1
regex ×1
visibility ×1