小编Met*_*etz的帖子

Class <T>和静态方法Class.forName()让我发疯

此代码无法编译.我想知道我做错了什么:

private static Importable getRightInstance(String s) throws Exception {
 Class<Importable> c = Class.forName(s);
 Importable i = c.newInstance();
 return i;
}
Run Code Online (Sandbox Code Playgroud)

其中Importable是接口,字符串s是实现类的名称.编译器说:

./Importer.java:33: incompatible types
found   : java.lang.Class<capture#964 of ?>
required: java.lang.Class<Importable>
  Class<Importable> c = Class.forName(format(s));
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

所有的解决方案

Class<? extends Importable> c = Class.forName(s).asSubclass(Importable.class);
Run Code Online (Sandbox Code Playgroud)

Class<? extends Importable> c = (Class<? extends Importable>) Class.forName(s);
Run Code Online (Sandbox Code Playgroud)

Class<?> c = Class.forName(format(s));
Importable i = (Importable)c.newInstance();
Run Code Online (Sandbox Code Playgroud)

给出这个错误(我不明白):

Exception in thread "main" java.lang.IncompatibleClassChangeError: class C1 
has interface Importable as super class
Run Code Online (Sandbox Code Playgroud)

其中C1实际上是实现可导入的(因此它理论上可以转换为可导入的).

java generics reflection

10
推荐指数
2
解决办法
8927
查看次数

二进制搜索树的删除过程

当要删除的节点有两个子节点时,请考虑BST上的删除过程.假设我总是用在其右子树中保持最小键的节点替换它.

问题是:这个程序是可交换的吗?也就是说,删除x然后y与删除第一个y然后x?

我认为答案是否定的,但我找不到反例,也没有找出任何有效的推理.

编辑:

也许我必须更清楚.

考虑以下transplant(node x, node y)过程:将x替换为y(及其子树).所以,如果我想删除一个有两个子节点的节点(比如说x),我用它右边子树中保存最小键的节点替换它:

y = minimum(x.right)
transplant(y, y.right) // extracts the minimum (it doesn't have left child)
y.right = x.right
y.left = x.left
transplant(x,y)
Run Code Online (Sandbox Code Playgroud)

问题是如何证明上述程序不是可交换的.

algorithm binary-tree binary-search-tree data-structures

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

实现Java Iterable <E>接口

公共类C1实现Iterable {private LinkedList list; public static class NC1 {...} ... x public Iterator iterator(){return list.iterator(); }}

但是日食呜呜声(在x-ed线上):

- The return type is incompatible with Iterable<NC1>.iterator()
- implements java.lang.Iterable<NC1>.iterator
Run Code Online (Sandbox Code Playgroud)

我不明白错误在哪里.有人可以帮忙吗?

java iterable

5
推荐指数
1
解决办法
3292
查看次数

Linux内核链表实现的list_entry()中的(char*)强制转换

这是宏定义:

/**
 * list_entry - get the struct for this entry
 * @ptr:    the &struct list_head pointer.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
    ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
Run Code Online (Sandbox Code Playgroud)

我不明白为什么ptr被铸造(char *).我不能只是减去偏移的member距离ptr?像这样:

#define list_entry(ptr, type, member) \
        ((type *)((ptr)-(unsigned long)(&((type *)0)->member)))
Run Code Online (Sandbox Code Playgroud)

谢谢!

c

4
推荐指数
1
解决办法
398
查看次数

围绕枢轴交换选择

(不是等号周围交换文本的可能重复:)

我经常发现自己在交换东西.这是屁股的痛苦.

我写完这段代码之后就说吧

tmp = realloc (words, sizeof (char **) * (*count + 1));
Run Code Online (Sandbox Code Playgroud)

我注意到连续的星号太多了,不喜欢它,并希望在乘法星号周围交换这两个因子.

或者,我写

#if !defined(_CONSOLE_H_) && defined (__MINGW32__)
Run Code Online (Sandbox Code Playgroud)

但我突然意识到,defined (__MINGW32__)出于某种原因,必须先行.

我想如果我可以做这样的事情会很酷:

(在一个字符上x,[x]表示光标位置.<S>代表这种假设的"交换"命令)

#if [!]defined(_CONSOLE_H_) && defined (__MINGW32__)
Run Code Online (Sandbox Code Playgroud)

command:vf&<S>$=>选择从光标到pivot(单词&&),并将选择与文本交换到行尾.

或者,对于前一个例子:

tmp = realloc (words, [s]izeof (char **) * (*count + 1));
Run Code Online (Sandbox Code Playgroud)

command:v3f*<S>f)=>从这里选择到第三个*,用文本转发到).

对我来说,这将是非常有用的.有没有这样的东西,或者我必须编写自己的插件?

谢谢!

编辑 -

作为@ib.他在回答的评论中说,我需要更具体地说明什么是支点.

枢轴也可以是一个角色,例如:

static char ** tokenize_input (char * …
Run Code Online (Sandbox Code Playgroud)

vim

4
推荐指数
1
解决办法
502
查看次数