我想根据长度对java中的字符串进行子串(起始字符)。例如,如果 string1 大于 4000 字节,我想将该字符串变成小于或等于 4000 字节的字符串。(需要修剪起始字符而不是最后一个字符)
下面是比较两个不同列表实现的程序.当我运行它时,结果是真的.
在这种情况下,我想了解这个equals()方法的行为.
package com.tests;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List listA = new ArrayList<>();
List listB = new LinkedList<>();
Object object = new Object();
Integer integer = new Integer(4);
listA.add(object);
listB.add(object);
System.out.println(listA.equals(listB));
}
}
Run Code Online (Sandbox Code Playgroud) 代码片段如下。
我知道 String 的默认值是null并且它被分配给str. 我不明白的是为什么null在第一种情况下打印而不在其他情况下打印(如第二个代码片段)。
public class Case1{
static String str;
public static void main(String[] args){
System.out.println(str);
}
}
\\Code prints 'null' without quotes
Run Code Online (Sandbox Code Playgroud)
public class Case2{
public static void main(String[] args){
String a[][] = { {}, null };
System.out.println(a[1][0]);
}
}
\\Code throws nullPointerException
Run Code Online (Sandbox Code Playgroud)
任何解释将不胜感激。
我正在尝试制作加密程序.
如何在第100行摆脱"语法错误,插入"最后"完成BlockStatements"
<imports>
public class Afp {
...
/**
* Initialize the contents of the frame.
*/
private void initialize() {
....
JButton btnEncrypt = new JButton("Encrypt");
btnEncrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String text;
StringBuffer passWord = new StringBuffer(""+ text);
for(int i = 0; i < passWord.length(); i++){
int temp = 0;
temp = (int)passWord.charAt(i);
temp = temp*9834 / 8942 /33 *90023243 * 9 +124324534 - 2335 *24324;
passWord.setCharAt(i, (char)temp);
}
}
}
});
...
} …Run Code Online (Sandbox Code Playgroud) public class Str {
public static void main(String[] args) {
String str = "abcde";
String s = str.substring(str.length());
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
字符'e'的索引为4,但我试图获取整个长度为5的字符串。如果执行上述代码,为什么不抛出IndexOutOfBoundsException??
如何检查字符串是否等于"}"?
我试过了
str_.equals("\\}");
Run Code Online (Sandbox Code Playgroud)
和
str_.equals("[}]");
Run Code Online (Sandbox Code Playgroud)
他们似乎都没有工作.