当我有这样的方法:
public static void foo(String param) throws IOException
{
try
{
// some IOoperations
if (param.isEmpty())
{
throw new IOException("param is empty");
}
// some other IOoperations
} catch (Exception e) {
/* handle some possible errors of of the IOoperations */
}
}
Run Code Online (Sandbox Code Playgroud)
当抛出IOException("param为空")时,它会被该try-catch主体捕获.但是此异常适用于此方法的调用者.我该怎么做呢?是否有"pure-Java"这样做或者我是否必须创建另一种类型的Exception,它不是IOException的实例以避免try-catch体将处理它?
我知道IllegalArgumentException在这种情况下你会建议使用a .但这是我情况的简化示例.事实上,我抛出的异常是一个IOException.
谢谢
我想完成这个人做的事情:
但是我需要优化它以超快速运行.简而言之,我想要一个时间序列,并告诉每次它越过零(改变符号).我想记录过零点之间的时间.由于这是真实的数据(32位浮点数),我怀疑我每个都有一个正好为零的数字,所以这并不重要.我目前有一个计时程序,所以我会把你的结果计算在内,看看谁赢了.
我的解决方案给出(微秒):
open data 8384
sign data 8123
zcd data 415466
Run Code Online (Sandbox Code Playgroud)
如您所见,过零检测器是缓慢的部分.这是我的代码.
import numpy, datetime
class timer():
def __init__(self):
self.t0 = datetime.datetime.now()
self.t = datetime.datetime.now()
def __call__(self,text='unknown'):
print text,'\t',(datetime.datetime.now()-self.t).microseconds
self.t=datetime.datetime.now()
def zcd(data,t):
sign_array=numpy.sign(data)
t('sign data')
out=[]
current = sign_array[0]
count=0
for i in sign_array[1:]:
if i!=current:
out.append(count)
current=i
count=0
else: count+=1
t('zcd data')
return out
def main():
t = timer()
data = numpy.fromfile('deci.dat',dtype=numpy.float32)
t('open data')
zcd(data,t)
if __name__=='__main__':
main()
Run Code Online (Sandbox Code Playgroud) 该USPS提供一个C库,并产生自己的新的智能邮件条形码Java包装.有没有人找到一个Perl包装?我曾经使用过一次SWIG,但想知道在我遇到麻烦之前是否已经完成了.到目前为止,我的搜索只会导致那些假装拥有您搜索条件的垃圾网站.
我得到了例外
'Moq.Mock`1'的类型初始值设定项引发了异常.
使用Moq 4.0我已经在几个论坛上查看过,他们提到使用Moq-NoCastle版本.我在Moq文件夹中尝试了这个和版本.两者都有相同的结果.
我有一个包含2个项目的解决方案,一个用于我的界面,一个用于我的测试.我的主项目有2个文件:
IMyInterface.cs:
using System;
namespace Prototype
{
public interface IMyInterface
{
int Value { get; set; }
void ProcessValue();
int GetValue();
}
}
Run Code Online (Sandbox Code Playgroud)
我的program.cs文件只包含项目生成的默认代码.
我的测试项目有一个单独的文件用于我的虚拟测试 - TestProgram.cs
using System;
using NUnit.Framework;
using Moq;
namespace Prototype.UnitTests
{
[TestFixture]
public class TestProgram
{
Mock<IMyInterface> mock;
[TestFixtureSetUp]
void TestSetup()
{
mock = new Mock<IMyInterface>();
mock.Setup(x => x.GetValue()).Returns(2);
}
[Test]
public void RunTest()
{
IMyInterface obj = mock.Object; /* This line fails */
int val = obj.GetValue();
Assert.True(val == 2); …Run Code Online (Sandbox Code Playgroud) 有一点我没想到并且谷歌没有帮助我,为什么可能与共享内存存在冲突,而不是全局内存?与寄存器存在银行冲突吗?
更新 哇我非常感谢Tibbit和Grizzly的两个答案.看来我只能在一个答案上给出绿色复选标记.堆栈溢出我是新手.我想我必须选择一个最好的答案.我可以做点什么来说谢谢答案我不给绿色支票吗?
这是上下文:
我尝试编写一个映射器,用于动态地将我的DomainModel对象转换为ViewModel Ojects.我得到的问题是,当我尝试通过反射调用泛型类的方法时,我得到了这个错误:
System.InvalidOperationException:无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作.
有人可以帮助我找出错误在哪里吗?这将不胜感激
这是代码(我试图简化它):
public class MapClass<SourceType, DestinationType>
{
public string Test()
{
return test
}
public void MapClassReflection(SourceType source, ref DestinationType destination)
{
Type sourceType = source.GetType();
Type destinationType = destination.GetType();
foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
{
string destinationPropertyName = LookupForPropertyInDestinationType(sourceProperty.Name, destinationType);
if (destinationPropertyName != null)
{
PropertyInfo destinationProperty = destinationType.GetProperty(destinationPropertyName);
if (destinationProperty.PropertyType == sourceProperty.PropertyType)
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
else
{
Type d1 = typeof(MapClass<,>);
Type[] typeArgs = { destinationProperty.GetType(), sourceType.GetType() };
Type constructed = …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个Trie但是在具有非常有限的内存容量的移动电话上.
我认为最好将整个结构存储在磁盘上,并且只在必要时才加载,因为我可以容忍一些磁盘读取.但是,经过几次尝试后,这似乎是一件非常复杂的事情.
有什么方法可以将Trie存储在磁盘上(即仅部分加载)并保持快速查找属性?
这开始是一个好主意吗?
我有一个正在进行大量文本处理的类.对于每个字符串,其长度为100-> 2000个字符,我正在执行30个不同的字符串替换.
例:
string modified;
for(int i = 0; i < num_strings; i++){
modified = runReplacements(strs[i]);
//do stuff
}
public runReplacements(String str){
str = str.replace("foo","bar");
str = str.replace("baz","beef");
....
return str;
}
Run Code Online (Sandbox Code Playgroud)
'foo','baz'和所有其他"目标"只会出现一次,并且是字符串文字(不需要实际的正则表达式).
你可以想象,我关注性能:)
鉴于这种,
replaceFirst()似乎是一个糟糕的选择,因为它不会使用Pattern.LITERAL,并将进行不需要的额外处理.
replace() 似乎是一个糟糕的选择,因为它将遍历整个字符串,寻找要替换的多个实例.
另外,由于我的替换文本每次都是相同的,所以我似乎有意义地编写自己的代码,String.replaceFirst()或者在后台每次都String.replace()做Pattern.compile一次.我想我应该编写自己的代码,这是我的想法:
每个文字替换Pattern.compile() 只需执行一次(无需每次重新编译)(即p1 - p30)
然后为每个pX执行以下操作: p1.matcher(str).replaceFirst(Matcher.quoteReplacement("desiredReplacement"));
这样我放弃第一次替换(不是遍历整个字符串),我使用文字与正则表达式,我不是每次迭代都重新编译.
那么,哪种性能最佳?