为什么当我围绕此代码包装SwingWorker时,它不再报告抛出异常?
import java.security.InvalidParameterException;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
IntegerString s = new IntegerString("EIGHT");
return null;
}
}.execute();
}
});
}
}
class IntegerString {
public IntegerString(String s) {
if (!isInteger(s)) {
System.out.println("...throwing exception.");
throw new InvalidParameterException("Thrown.");
}
// ...
}
static boolean isInteger(String str) {
if (str == null) {
return false; …Run Code Online (Sandbox Code Playgroud) 为什么在Java中,变量的范围局限于开关块而不是case块.例如,
// Scope limited to a switch block
switch (number) {
case 1:
String result = "...";
break;
case 2:
result = "...";
break;
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,结果只需要声明一次.如果您声明两次,则会收到一条Duplicate local variable消息.
我的问题是:程序如何知道你已经声明result,如果number = 2?(它不会落入case 1并且不会声明变量......或者它会吗?)
编辑:
我可能会让所有人感到困惑.我理解如何限制变量的范围,但我的问题是:如果不属于这种情况,Java如何知道结果已被声明?
我有很多来自音乐家网站的.jpeg.这些图像由即将上映的海报和乐队照片(现实生活中的乐队照片)组成.
这是一个示例海报:

我不熟悉任何现代技术或算法(如果它们存在?),但这是我认为我可能会寻找的:
是否有任何分类算法可以检测图像是否是海报?
artificial-intelligence classification machine-learning image-processing
我正在编写Vector和Matrix依赖类型的数据类型.
data Vector n e where
EmptyVector :: Vector Zero e
(:>) :: e -> Vector n e -> Vector (Succ n) e
deriving instance Eq e => Eq (Vector n e)
infixr :>
data Matrix r c e where
EmptyMatrix :: Matrix Zero c e
(:/) :: Vector c e -> Matrix r c e -> Matrix (Succ r) c e
deriving instance Eq e => Eq (Matrix r c e)
infixr :/
Run Code Online (Sandbox Code Playgroud)
它们取决于自然数,也取决于类型.
data …Run Code Online (Sandbox Code Playgroud) 我遇到了解Java接口语义的问题.我在用代码填充之前用接口问题建模.我这样做是通过找出问题中的名词并为它们创建一个界面,例如:
每个房子里面至少有一个人.每个房子也可能只包含一辆自行车,可能至少包含一辆汽车.
因此,我将创建IHouse,IPerson和IVehicle接口(因为我们将来可能会有不同类型的房屋,人员和车辆).
当我进入这些名词的形容词时,问题出现了,例如:
每个房子,自行车和汽车都可能有一定的颜色.
我创建了以下界面,而不是在每个车辆和房屋类中创建以下方法:
public interface IPainted {
public Paint getPaint();
public void setPaint();
}
Run Code Online (Sandbox Code Playgroud)
并将其应用于车辆和房屋界面:
public interface IVehicle extends IPainted {
}
Run Code Online (Sandbox Code Playgroud)
这是表示形容词的最佳方式吗?
我正在使用EclEmma(在Eclipse内部)来扫描我的JUnit代码覆盖率.这工作正常 - 但是,我不希望EclEmma扫描我的src/view文件夹,因为它包含我认为不值得测试的Swing代码.
当EclEmma运行时,是否有任何方法可以忽略此文件夹:a)运行速度更快,b)不会扭曲覆盖百分比?
编辑:
我的项目结构是:
src/view
src/model
src/controller
Run Code Online (Sandbox Code Playgroud)
我已尝试使用"首选项"页面中的"路径条目"部分(可能还有其他):
"src/view"
"src/view/*"
"view"
"view/*"
src/view
Run Code Online (Sandbox Code Playgroud)
这些是使用"首选项"页面中的"排除"部分:
*
*View*
*View*.class
src/view/*View*
src/view/*View*.class
Run Code Online (Sandbox Code Playgroud)
他们都给我留下了与分析我整个src文件夹相同的结果.
我正在使用Python和Scrapy来解决这个问题.
我正在尝试抓取网页A,其中包含指向网页B1,B2,B3,...的链接列表.每个B页面都包含指向另一个页面的链接,C1,C2,C3,...,其中包含图像.
因此,使用Scrapy,伪代码中的想法是:
links = getlinks(A)
for link in links:
B = getpage(link)
C = getpage(B)
image = getimage(C)
Run Code Online (Sandbox Code Playgroud)
但是,在尝试解析Scrapy中的多个页面时,我遇到了问题.这是我的代码:
def parse(self, response):
hxs = HtmlXPathSelector(response)
links = hxs.select('...')
items = []
for link in links:
item = CustomItem()
item['name'] = link.select('...')
# TODO: Somehow I need to go two pages deep and extract an image.
item['image'] = ....
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
(注意:我的问题类似于在Scrapy项目中使用多个蜘蛛,但我不确定如何从Scrapy的Request对象中"返回"值.)
将解析树(即具体语法树)简化为抽象语法树的一般策略是什么?
例如,我有以下语法规则:
statement_list : statement
| statement_list statement
Run Code Online (Sandbox Code Playgroud)
如果保留为解析树,它将生成看起来像扇形输出
program
statement_list
statement_list
statement
definition
p_type
assignment
statement
definition
statement
assign
assignment
Run Code Online (Sandbox Code Playgroud)
如果我连接每个节点的子节点(因为语句列表在解析后没有固有含义),我可以实现以下内容
program
definition
p_type
assignment
definition
assign
assignment
Run Code Online (Sandbox Code Playgroud)
这很好用 - 但是,我没有意识到这样做的任何"规则".是否有特定的语法规则我应该简化?这是一种感觉问题,还是一个更机械化的过程?
compiler-construction grammar parsing abstract-syntax-tree concrete-syntax-tree
我正在重新使用C,但我已被其他语言的泛型所破坏.我在可调整大小的数组的实现中使用了以下代码:
typdef struct {
void** array;
int length;
int capacity;
size_t type_size;
} Vector;
void vector_add(Vector* v, void* entry) {
// ... code for adding to the array and resizing
}
int main() {
Vector* vector = vector_create(5, sizeof(int));
vector_add(vector, 4); // This is erroneous...
// ...
}
Run Code Online (Sandbox Code Playgroud)
在我试图使这个泛型,我现在无法向向量添加一个整数而不将其存储在其他地方的内存中.
有没有办法让这项工作(既可以是原样,也可能是更好的仿制药方法)?
我正在用Python编写一个Chess程序,需要生成一个骑士的所有动作.对于那些不熟悉国际象棋的人来说,骑士会以L形移动.
因此,考虑的位置,(2, 4)骑士可以移动到(0, 3),(0, 5),(1, 2),(3, 2至多)八种不同的移动共()等.
我想编写一个名为的函数knight_moves,在列表中生成这些元组.在Python中最简单的方法是什么?
def knight_moves(position):
''' Returns a list of new positions given a knight's current position. '''
pass
Run Code Online (Sandbox Code Playgroud)