我使用以下代码:
if (delanaloge.equals(stari)) {
if (novi.equals("-")) {
zdruzen = " -";
} else {
zdruzen = zdruzen + " " + " - " + novi + "\r";
}
nap = true;
}
Run Code Online (Sandbox Code Playgroud)
\r附加以创建换行符,但它不会像我期望的那样生成换行符.我想生成类似于此的输出:
- 213
- 232
- 1321
Run Code Online (Sandbox Code Playgroud)
如何在字符串中添加换行符?
我正在编写一个函数,可以调用自己大约5000次.当然,我得到了一个StackOverflowError.有什么方法可以用相当简单的方式重写这段代码吗?:
void checkBlocks(Block b, int amm) {
//Stuff that might issue a return call
Block blockDown = (Block) b.getRelative(BlockFace.DOWN);
if (condition)
checkBlocks(blockDown, amm);
Block blockUp = (Block) b.getRelative(BlockFace.UP);
if (condition)
checkBlocks(blockUp, amm);
//Same code 4 more times for each side
}
Run Code Online (Sandbox Code Playgroud)
那么,我们可以称之为功能的深度有多大限制?
我得到用户输入,包括非ASCII字符和不可打印的字符,例如
\xc2d
\xa0
\xe7
\xc3\ufffdd
\xc3\ufffdd
\xc2\xa0
\xc3\xa7
\xa0\xa0
Run Code Online (Sandbox Code Playgroud)
例如:
email : abc@gmail.com\xa0\xa0
street : 123 Main St.\xc2\xa0
Run Code Online (Sandbox Code Playgroud)
期望的输出:
email : abc@gmail.com
street : 123 Main St.
Run Code Online (Sandbox Code Playgroud)
使用Java删除它们的最佳方法是什么?
我试过以下,但似乎没有用
public static void main(String args[]) throws UnsupportedEncodingException {
String s = "abc@gmail\\xe9.com";
String email = "abc@gmail.com\\xa0\\xa0";
System.out.println(s.replaceAll("\\P{Print}", ""));
System.out.println(email.replaceAll("\\P{Print}", ""));
}
Run Code Online (Sandbox Code Playgroud)
产量
abc@gmail\xe9.com
abc@gmail.com\xa0\xa0
Run Code Online (Sandbox Code Playgroud) 实现java.awt.event.ActionListener界面的最佳方法是什么?
让您的类实现ActionListener并将其添加为ActionListener:
class Foo implements ActionListener{
public Foo() {
JButton button = new JButton();
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
}
}
Run Code Online (Sandbox Code Playgroud)
或者添加匿名ActionListener类的对象:
class Foo{
public Foo() {
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}
Run Code Online (Sandbox Code Playgroud) 我想写一个测试IndexOutOfBoundsException.请记住,我们应该使用JUnit 3.
我的代码:
public boolean ajouter(int indice, T element) {
if (indice < 0 || indice > (maListe.size() - 1)) {
throw new IndexOutOfBoundsException();
} else if (element != null && !maListe.contains(element)) {
maListe.set(indice, element);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
经过一些研究,我发现你可以使用JUnit 4来实现这一点,@Test(expected = IndexOutOfBoundsException.class)但是我没有在JUnit 3中找到如何做到这一点.
如何使用JUnit 3进行测试?
我正在学习在线java课程,使用Java编程简介.
在I/O章节中,下面的代码引入了以下语句:
顺便说一句,在本程序结束时,您将在try语句中找到我们的第一个有用的finally子句示例.当计算机执行try语句时,无论如何都保证finally子句中的命令被执行.
该程序位于第11.2.1节的末尾,是一个简单的程序,只读取文件中的一些数字并以相反的顺序写入.
main方法中的相关代码是(data是Reader,result是Writer):
try {
// Read numbers from the input file, adding them to the ArrayList.
while ( data.eof() == false ) { // Read until end-of-file.
double inputNumber = data.getlnDouble();
numbers.add( inputNumber );
}
// Output the numbers in reverse order.
for (int i = numbers.size()-1; i >= 0; i--)
result.println(numbers.get(i));
System.out.println("Done!");
} catch (IOException e) {
// Some problem reading the data from the input file.
System.out.println("Input Error: " + e.getMessage()); …Run Code Online (Sandbox Code Playgroud) Collections.shuffle()经过一个Collection向后的每个索引,然后用随机索引交换它,包括或之前.我想知道为什么,所以我尝试做同样的事情,但交换任何随机索引Collection.
这是Collections.shuffle()代码的混乱部分:
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
Run Code Online (Sandbox Code Playgroud)
这是我的算法:
Random r = new Random();
for (int i = 0; i < a.size(); i++) {
int index = r.nextInt(a.size());
int temp = a.get(i);
a.set(i, a.get(index));
a.set(index, temp);
}
Run Code Online (Sandbox Code Playgroud)
我发现Collections.shuffle()当我同时运行两ArrayList百万次时,我的代码分布比我的代码更均匀.此外,在运行我的代码时:
[0,1,2,3,4]
似乎以下排列最常出现:
[
1,0,3,4,2]
[1,2,3,4,0]
[1,2,0,4,3 ] [0,2,3,4,1]
[1,2,3 ] ,0,4]
有人可以解释一下原因吗?
我一直在尝试获取列表的子列表,将其反转,然后将反转的列表放回起始位置。例如,假设我们拥有列表[1, 2, 3, 4, 5, 6],然后从索引2反转到索引4将得到[1, 2, 5, 4, 3, 6]。
我为此编写了一些代码,但是ConcurrentModificationException每次都给出一个代码(除非startIndex == endIndex)。下面提供了一个最小的可重现示例:
int startIndex = 2;
int endIndex = 4;
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
List<Integer> toReverse = list.subList(startIndex, endIndex+1);
Collections.reverse(toReverse);
list.removeAll(toReverse);
list.addAll(startIndex, toReverse);
Run Code Online (Sandbox Code Playgroud)
异常线程“main” java.util.ConcurrentModificationException
在java.util.ArrayList中的$ SubList.checkForComodification(来源不明)
在java.util.ArrayList的$ SubList.size(来源不明)在
java.util.AbstractCollection.toArray(来源不明)在 test.ConcurrentExample.main(ConcurrentExample.java:64)处
java.util.ArrayList.addAll(Unknown Source
)
错误所指的实际行是list.addAll(startIndex, toReverse);。
我不确定是什么问题,因为迭代过程中似乎没有任何变化。如果有人能解释为什么会这样和/或如何解决它,将不胜感激。
我正在尝试根据行为驱动开发进行编程,该开发表明,如果不首先编写失败的单元测试,则不应编写任何代码行.
我的问题是,如何将BDD与私有方法一起使用?
我如何对私有方法进行单元测试?
有没有更好的解决方案:
- 首先将私有方法公开,然后在编写使用这些私有方法的公共方法时将其设为私有;
或者
- 在C#中使所有私有方法都在内部并使用InternalsVisibleTo属性.
public synchronized X getAnotherX(){
if(iterator.hasNext()){
X b = iterator.next();
String name = b.getInputFileName();
...
return b;
}
else{return null;}
}
Run Code Online (Sandbox Code Playgroud)
尽管声明头中的synchronized语句,我仍然在我使用iterator.next()的行中得到一个ConcurrentModificationException异常; 什么错了?
java ×9
arraylist ×1
bdd ×1
collections ×1
concurrency ×1
exception ×1
iterator ×1
junit ×1
junit3 ×1
shuffle ×1
string ×1
swing ×1
tdd ×1
unit-testing ×1