我不理解泛型和数组之间的联系.
我可以使用泛型类型创建数组引用:
private E[] elements; //GOOD
Run Code Online (Sandbox Code Playgroud)
但无法使用泛型类型创建数组对象:
elements = new E[10]; //ERROR
Run Code Online (Sandbox Code Playgroud)
但它有效:
elements = (E[]) new Object[10]; //GOOD
Run Code Online (Sandbox Code Playgroud) 我想创建一个带有实用程序方法的类
public class Util {
public static void f (int i) {...}
public static int g (int i, int j) {...}
}
Run Code Online (Sandbox Code Playgroud)
哪个是创建实用程序类的最佳方法?
我应该使用私有构造函数吗?
我应该为抽象类创建实用程序类吗?
我什么都不做?
我有一个带有上限泛型的列表.
List<? extends Number> l = new ArrayList<>();
l.add(new Integer(3)); //ERROR
l.add(new Double(3.3)); // ERROR
Run Code Online (Sandbox Code Playgroud)
我不明白这个问题,因为Integer和Double扩展了Number.
我有一个JSF:Primefaces SelectCheckBoxMenu
<p:selectCheckboxMenu value="#{domain.listaa}" label="Chooese!" style="height:25px" showCheckbox="true">
<p:ajax update="records" listener="#{domain.muti}" />
<f:selectItems value="#{domain.recLabels}"/>
</p:selectCheckboxMenu>
Run Code Online (Sandbox Code Playgroud)
在托管bean中:
private boolean[] recFlags = new boolean[]{true,true,true,true,true,true,true};
private String[] recLabels = new String[]{"A","AAAA","MX","NS","SOA","CNAME","TXT"};
private List<String> listaa = new ArrayList<>();
public void muti(AjaxBehaviorEvent event){
Arrays.fill(recFlags, false);
for(int i=0;i<recLabels.length;i++){
if(listaa.contains(recLabels[i])){
recFlags[i]=true;
}
}
System.out.println(listaa.toString());
}
Run Code Online (Sandbox Code Playgroud)
所以在SelectCheckBoxMenu我按任意按钮,ajax调用正在运行,muti()函数将运行.没有问题.但是,如果我按下SelectCheckboxMenu中的"全选"(最上面)按钮,则ajax调用不起作用,muti()函数将无法运行,listaa(按下复选框的列表)不会改变.为什么?我如何解决,"全选"按钮有效?
我有tyo字节变量
byte a = 3;
byte b = 4;
Run Code Online (Sandbox Code Playgroud)
如果我求和它们,sum的值是整数.
byte z = a+b //error, left side is byte, right side is integer
Run Code Online (Sandbox Code Playgroud)
为什么a + b是int?
我想用Swing创建一个简单的桌面游戏.我有一个JFrame和一个JPanel变量.
我想将JButtons添加到这个JPanel,但我想创建一个自己的类.
我创建了一个扩展JButton(继承)的类:
public class GameField extends JButton {...}
Run Code Online (Sandbox Code Playgroud)
所以我可以将GameField添加到JPanel.
但我想通过组合创建GameFields:
public class GameField{
private JButton button;
}
Run Code Online (Sandbox Code Playgroud)
但在这篇文章中我如何将GameField添加到JPanel?我可以通过compisition解决这个问题吗?
我有一些非常相似的功能,但每个功能中有一行不同.
如何避免代码重复?
public class Example{
public void f(){
System.out.println("Start");
OtherExample.start();
AnotherExample.funct1(); //DIFFERENT CODE LINE
OtherExample.end();
System.out.println("End");
}
public void g(){
System.out.println("Start");
OtherExample.start();
AnotherExample.funct2(); //DIFFERENT CODE LINE
OtherExample.end();
System.out.println("End");
}
public void h(){
System.out.println("Start");
OtherExample.start();
AnotherExample.funct3(); //DIFFERENT CODE LINE
OtherExample.end();
System.out.println("End");
}
public void i(){
System.out.println("Start");
OtherExample.start();
AnotherExample.funct4(); //DIFFERENT CODE LINE
OtherExample.end();
System.out.println("End");
}
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我一些合适的设计模式吗?
我有一个泛型工厂方法.
public static <T> List<T> create(){
return new ArrayList<T>();
}
Run Code Online (Sandbox Code Playgroud)
但它没有参数/参数.
我没有为函数提供类型参数,但它知道没有任何参数的适当类型.
public static void main(String[] args){
List<Integer> intlist = create(); //it is an Integer List
List<String> stringlist = create(); //it is a String List
}
Run Code Online (Sandbox Code Playgroud) java ×7
generics ×2
ajax ×1
byte ×1
inheritance ×1
integer ×1
jsf ×1
primefaces ×1
swing ×1
utility ×1