我有几个课程,如下所示
public class TrueFalseQuestion implements Question{
static{
QuestionFactory.registerType("TrueFalse", "Question");
}
public TrueFalseQuestion(){}
}
Run Code Online (Sandbox Code Playgroud)
...
public class QuestionFactory {
static final HashMap<String, String > map = new HashMap<String,String>();
public static void registerType(String questionName, String ques ) {
map.put(questionName, ques);
}
}
public class FactoryTester {
public static void main(String[] args) {
System.out.println(QuestionFactory.map.size());
// This prints 0. I want it to print 1
}
}
Run Code Online (Sandbox Code Playgroud)
如何更改TrueFalseQuestion
类以便始终运行静态方法,以便在运行main方法时得到1而不是0?我不希望主方法有任何改变.
我实际上是在尝试实现子类向工厂注册的工厂模式,但我已经简化了这个问题的代码.
我正在尝试从我的表中处理数百万条记录(大小约为30 GB),我目前正在使用分页(mysql 5.1.36).我在for循环中使用的查询是
select blobCol from large_table
where name= 'someKey' and city= 'otherKey'
order by name
LIMIT <pageNumber*pageSize>, <pageSize>
Run Code Online (Sandbox Code Playgroud)
这适用于大约500K记录.我使用的页面大小为5000,在第100页之后,查询开始显着减慢.第一〜80页中提取2-3秒,但大约130页之后,每个页面大约需要30秒来检索,至少要等到页面200我的一个查询的大约有900页,这将花费太长时间.
The table structure is (type is MyISAM)
name char(11)
id int // col1 & col2 is a composite key
city varchar(80) // indexed
blobCol longblob
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能加快速度?查询的解释显示了这一点
select_type: SIMPLE
possible_keys: city
key : city
type: ref
key_len: 242
ref: const
rows: 4293720
Extra: using where; using filesort
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,我的服务器my.cnf(24 GB RAM,2四核处理)有这些条目
key_buffer_size = 6144M
max_connections = 20
max_allowed_packet = 32M
table_open_cache = …
Run Code Online (Sandbox Code Playgroud) 我在python中有一个包含一组值的数组,其中一些是
2.32313e + 07
2.1155e + 07
1.923e + 07
11856
112.32
如何将指数格式转换为十进制格式
附加:在使用awk在UNIX中打印时,有没有办法可以将指数直接转换为十进制?
有没有一种好的算法来检查行或列中是否有5个相同元素或对角线给出一个6x6的方阵?
当然,有一种天真的算法:遍历每个点,然后针对矩阵中的每个点,遍历该行,col,然后遍历对角线。我想知道是否有更好的方法。
我有一段代码可以正常使用MSVC,但无法使用clang ++进行编译
void MyCass::someMethod()
{
std::wstring key(...);
auto& refInstance = m_map.find(key); // error here
}
Run Code Online (Sandbox Code Playgroud)
其中m_map定义为
std::map<const std::wstring, std::shared_ptr<IInterface>> m_map;
Run Code Online (Sandbox Code Playgroud)
和clang抱怨
non-const lvalue reference cannot bind to incompatible temporary
Run Code Online (Sandbox Code Playgroud)
我有点明白,临时正在创建,但不知道如何解决这个问题.有任何想法吗?
我正在阅读别人写的Perl脚本,我对Perl不太熟悉所以有人能让我知道前三行做了什么?
my $ref = do($filename);
$ref != 0 or die "unable to read/parse $filename\n";
@varLines=@{$ref};
foreach $ord (@varLines)
{
# code here
}
Run Code Online (Sandbox Code Playgroud)
在$filename
获取命令行参数设置之后,这是在程序的开头
传递给此脚本的文件格式为
[
{
"Key1" => "val1",
"key2" => " "A",
},
{
"Key3" => "val2",
"key4" => " "B",
},
]
Run Code Online (Sandbox Code Playgroud) 我有一些.h文件如下(在Linux上)
Source/Server/connect.h
Source/Server/message.h
...
Run Code Online (Sandbox Code Playgroud)
我正在开发另一个需要两个.h文件但位于不同目录中的应用程序
Source/App2/..
Run Code Online (Sandbox Code Playgroud)
如何在app2应用程序中包含connect.h文件,考虑到我使用perforce并且其他所有在应用程序上工作的人都有自己的副本,所以添加一个绝对路径到包含库可能不是一个好主意但我不确定.
编辑:我使用专有的构建机制来构建代码,因此无法直接指定gcc选项.
编辑:澄清了一下这个问题
如何从具有该格式的字典中获取字符串
key1 = value1
key2 = value2
Run Code Online (Sandbox Code Playgroud)
以相对较快的方式?(相对于简单连接)
我正在尝试使用工厂模式来创建一个QuestionTypeFactory,其中实例化的类将像MultipleChoice,TrueFalseQuestion等.
工厂代码看起来像这样
class QuestionFactory {
public enum QuestionType {
TrueFalse,
MultipleChoice,
Essay
}
public static Question createQuestion(QuestionType quesType) {
switch (quesType) {
case TrueFalse:
return new TrueFalseQuestion();
case MultipleChoice:
return new MultipleChoiceQuestion();
case Essay:
return new EssayQuestion();
}
throw new IllegalArgumentException("Not recognized.");
}
}
Run Code Online (Sandbox Code Playgroud)
这个现在可行了.如果我想添加另一个问题类型,我将需要修改工厂类,我不想这样做.
如何设置它以便每个问题类在Factory中注册自己,这样当我添加新的问题类型时,我不必更改工厂的代码?我对java有点新,我不知道该怎么做.
附加信息
所有问题类都实现了IQuestion接口.我正在寻找一种方法来实现像这样的方法
public static void registerType(QuestionType quesType, Class<IQuestion> ques)
Run Code Online (Sandbox Code Playgroud)
这样我就可以从我的类中的静态块调用这个方法,这样当我添加一个新的问题类型时,我就不必在Question Factory中更改或添加任何代码了.我知道我必须更改当前的实现以使其通用.我不确定我上面写的方法在语法上是否正确,但它在概念中显示了我想要的东西.
为了 struct
typedef struct sharedData
{
sem_t *forks;
}sharedData;
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时,我收到警告:
sharedData sd;
sem_t forks[5];
sd.forks = &forks; // Warning: assignment from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
我误解或遗漏了什么吗?