小编Viv*_*ath的帖子

避免使用getfield操作码

在Java的String类中,trim方法包含:

int off = offset;      /* avoid getfield opcode */
char[] val = value;    /* avoid getfield opcode */
Run Code Online (Sandbox Code Playgroud)

我对评论"避免getfield操作码"感到有些困惑......

这是什么意思?(我认为这可以避免在字节码中使用getfield,但为什么这是一个Good Thing [TM]?)

难道是防止万一对象创建微调不会做任何事情(因此返回)或?

java bytecode opcode

30
推荐指数
2
解决办法
2569
查看次数

我应该何时使用Hashtable与HashMap

这不是一个关于之间的差异问题HashtableHashMap.我知道Hashtable对象不能接受null键或值条目的值,它是同步集合,并且它使用的内存略少于a HashMap.

我想知道使用a Hashtable而不是a 更合适的场景HashMap.

java collections hashtable hashmap

24
推荐指数
3
解决办法
2万
查看次数

在Javascript中抛出自定义异常.使用哪种款式?

Douglas Crockford建议做这样的事情:

throw {
    name: "System Error",
    message: "Something horrible happened."
};
Run Code Online (Sandbox Code Playgroud)

但你也可以这样做:

function IllegalArgumentException(message) {
    this.message = message;
}

throw new IllegalArgumentException("Argument cannot be less than zero");
Run Code Online (Sandbox Code Playgroud)

然后做:

try {
    //some code that generates exceptions
} catch(e) {    
    if(e instanceof IllegalArgumentException) {
        //handle this
    } else if(e instanceof SomeOtherTypeOfException) {
        //handle this
    }
}
Run Code Online (Sandbox Code Playgroud)

我想你可以type在Crockford的实现中包含一个属性,然后检查它而不是做一个instanceof.做一个对另一个有什么好处吗?

javascript exception-handling exception custom-exceptions

23
推荐指数
1
解决办法
7736
查看次数

为什么这个C代码编译?

#include <stdio.h>
int main() {
    int c = c;
    printf("c is %i\n", c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在定义一个名为的整数变量c,我将其值赋给自己.但这怎么可以编译呢?c尚未初始化,那么如何将其价值分配给自己呢?当我运行程序时,我明白了c is 0.

我假设编译器正在生成为c变量分配空间的汇编代码(当编译器遇到int c语句时).然后它接受未初始化空间中的任何垃圾值并将其分配回去c.这是发生了什么?

c variable-declaration

21
推荐指数
3
解决办法
842
查看次数

JavaScript创建对象的新实例

所以我正在设计一个成绩簿界面,我的课程定义为:

<script>
course = new Object();
 var name;
 var gradingareas;
 var finalgrade;
</script>
Run Code Online (Sandbox Code Playgroud)

然后我想创建一个新实例:

 var gradingareas = new Array("Homework", "Classwork", "Exams");

 course1 = new course("CS1500", gradingareas, 85);
Run Code Online (Sandbox Code Playgroud)

我也试过没有前面的var无济于事.我得到一个"未捕获的TypeError:对象不是一个函数"我是一个非常新的JavaScript,所以我甚至不知道我是否正确的方式.任何帮助表示赞赏谢谢.

javascript object instances

20
推荐指数
2
解决办法
8万
查看次数

无法在NSDictionary中使用"class"作为键

我正在尝试使用一个类作为一个关键NSDictionary.我看了这个问题的答案,我所拥有的几乎是一样的; 我正在使用setObject: forKey:.然而,XCode抱怨说Incompatible pointer types sending 'Class' to parameter of type 'id<NSCopying>'.我的电话是:

[_bugTypeToSerializerDictionary setObject: bugToStringSerializer 
                                forKey: [bugToStringSerializer serializedObjectType]];
Run Code Online (Sandbox Code Playgroud)

bugToStringSerializerBugToStringSerializer具体实现的实例serializedObjectType.具体实现的示例如下所示:

- (Class) serializedObjectType {
    return [InfectableBug class];
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

class objective-c nsdictionary

19
推荐指数
2
解决办法
5710
查看次数

进行SSL连接时,PKIX路径构建失败

我有一个商家帐户名为CommWeb整合和我发送一个SSL讯息到他们的URL(https://migs.mastercard.com.au/vpcdps).当我尝试发送帖子时,我得到以下异常:

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Run Code Online (Sandbox Code Playgroud)

执行帖子的代码(我没有编写,代码库中已经存在的代码)是:

public static HttpResponse sendHttpPostSSL(String url, Map<String, String> params) throws IOException {
    PostMethod postMethod = new PostMethod(url);
    for (Map.Entry<String, String> entry : params.entrySet()) {
        postMethod.addParameter(entry.getKey(), StringUtils.Nz(entry.getValue()));
    }

    HttpClient client = new HttpClient();
    int status = client.executeMethod(postMethod);
    if (status == 200) {
        StringBuilder resultBuffer = new StringBuilder();
        resultBuffer.append(postMethod.getResponseBodyAsString());
        return new HttpResponse(resultBuffer.toString(), "");
    } else {
        throw new IOException("Invalid response code: " + status); …
Run Code Online (Sandbox Code Playgroud)

java ssl integration ssl-certificate

17
推荐指数
2
解决办法
5万
查看次数

为什么我们有包含(Object o)而不是contains(E e)?

它是否与旧的(非泛化)版本保持向后兼容Collection?还是有一个我错过的更细微的细节?我看到这个模式在removealso(remove(Object o))中重复,但是add被泛化为add(E e).

java generics collections contains

17
推荐指数
2
解决办法
1647
查看次数

检查注释是否属于特定类型

我使用反射来看看是否被连接到一个类的属性的注释,是一种特定类型的.目前我在做:

if("javax.validation.Valid".equals(annotation.annotationType().getName())) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

这让我觉得有点笨拙,因为它依赖于一个完全限定的类名字符串.如果命名空间将来发生更改,则可能会导致细微错误.

我想要做:

if(Class.forName(annotation.annotationType().getName()).isInstance(
     new javax.validation.Valid()
)) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

但它javax.validation.Valid是一个抽象类,无法实例化.有没有办法模拟instanceof(或基本上使用isInstance)对接口或抽象类?

java reflection annotations instanceof isinstance

17
推荐指数
4
解决办法
2万
查看次数

使用SVG作为Android中的背景绘制

我正在尝试使用SVG图像(使用Inkscape创建并保存为普通SVG)作为我的应用程序的背景.我正在尝试使用该svg-android库.我有一个名为文件background.svgres/raw.我的代码看起来像这样:

SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.background);
Drawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);

LinearLayout backgroundLayout = (LinearLayout) findViewById(R.id.background);
bitmapDrawable.setTileModeX(Shader.TileMode.REPEAT);
backgroundLayout.setBackgroundDrawable(bitmapDrawable);
Run Code Online (Sandbox Code Playgroud)

但是,当我的应用程序启动时,没有任何内容显示为背景(除了布局中的背景颜色).我的布局xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#aacceeff"
    >

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    >
 </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

UPDATE

看来我的SVG存在问题.这可能是因为不支持所有功能.

svg android background

14
推荐指数
1
解决办法
3万
查看次数