为了访问Java中String的单个字符,我们有String.charAt(2).是否有任何内置函数可以删除java中字符串的单个字符?
像这样的东西:
if(String.charAt(1) == String.charAt(2){
//I want to remove the individual character at index 2.
}
Run Code Online (Sandbox Code Playgroud) 我的方法有一个抛出的返回类型NullPointerException.
public class Student {
public String studentOne() {
//code to get name
if(name == null)
throw new NullPointerException("Error");
else
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是..我应该public String studentOne throws NullPointerException在抛出新的异常时使用吗?
PS:我知道这不是最好的实践nullPointerException.但它在我的项目中是必需的
我是mockito的新手.我的问题是如何使用Mockito模拟for循环?
对于Eg:这是主类:
import java.util.HashSet;
import java.util.Set;
public class stringConcatination {
public static void main(String[] args) {
Set<String> stringSet = new HashSet();
stringSet.add("Robert");
stringSet.add("Jim");
for (String s:stringSet) {
s = "hi " + s;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试类:
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import static org.mockito.Mockito.mock;
public class stringConcatinationTest {
@Test
public void testMain() {
Set mockSet = mock(HashSet.class);
// -- How to mock For Loop --
}
}
Run Code Online (Sandbox Code Playgroud)
我看到了这个相关的问题.但我无法理解,for循环如何被嘲笑.