AFAIK有两种方法:
例如,
List<Foo> fooListCopy = new ArrayList<Foo>(fooList);
for(Foo foo : fooListCopy){
// modify actual fooList
}
Run Code Online (Sandbox Code Playgroud)
和
Iterator<Foo> itr = fooList.iterator();
while(itr.hasNext()){
// modify actual fooList using itr.remove()
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由偏好一种方法而不是另一种方法(例如,由于可读性的简单原因,更喜欢第一种方法)?
public class Foo {
public static void main(String[] args) {
float f;
System.out.println(f);
}
}
Run Code Online (Sandbox Code Playgroud)
print语句导致以下编译时错误,
局部变量f可能尚未初始化
如果Java中的原语已经有一个默认值(float = 0.0f),为什么我需要定义一个?
所以,这很有效
public class Foo {
float f;
public static void main(String[] args) {
System.out.println(new Foo().f);
}
}
Run Code Online (Sandbox Code Playgroud)
感谢大家!
有一段时间,当Gradle STS扩展程序在启动安装了Gradle STS扩展的Eclipse 3.7(Indigo)后尝试执行我的项目的gradle构建脚本时,我将收到以下错误,
Unable to start the daemon process. The exit value was: 1.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at http://gradle.org/docs/current/userguide/gradle_daemon.html
Please read below process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual …
Run Code Online (Sandbox Code Playgroud) 我想改变一个列表的内容JComboBox
(比如添加另一个列表代替旧的列表).有什么方法可以做到吗?提前致谢!
是否可以在gradle中嵌套任务,例如
task foo(dependsOn: jar){
// task 1
// task 2
// task 3
.
.
.
// task n
}
Run Code Online (Sandbox Code Playgroud)
其中执行顺序是jar
> foo
> task 1
> task 2
> task 3
> ... > task n
?我不希望将嵌套任务(即task 1
,,task 2
等等)暴露给用户。我只是想让foo
任务被曝光。
我使用下面的编码使用另一个jcombobox将值添加到jcombobox我需要根据jcombobox1中选择的一个值将值添加到jcombobox2而不附加值,所以有人可以告诉我重置或清除组合框的方法选择其他选项时的值?下面是我的编码,我是java和netbeans的新手,所以如果有人可以帮助我会感激不尽:)
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database1", "root", "senura123");
Statement stat = (Statement) con.createStatement();
String val=jComboBox1.getSelectedItem().toString();
String check; String col;
if ("Vehicles".equals(val)){
check = "select reg_no from vehicle;";
col="reg_no";
}
else if ("Travel Guides".equals(val)){
check = "select username from travelguide;";
col="username";
}
else{
check = "select username from transportofficer";
col="username";
}
ResultSet rslt = stat.executeQuery(check);
while (rslt.next()) {
jComboBox2.addItem(rslt.getString(col));
}
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码片段:
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
JPanel a = new JPanel();
a.setAlignmentX(Component.CENTER_ALIGNMENT);
a.setPreferredSize(new Dimension(100, 100));
a.setBorder(BorderFactory.createTitledBorder("aa"));
JPanel b = new JPanel();
b.setAlignmentX(Component.CENTER_ALIGNMENT);
b.setPreferredSize(new Dimension(50, 50));
b.setBorder(BorderFactory.createTitledBorder("bb"));
pane.add(a);
pane.add(b);
Run Code Online (Sandbox Code Playgroud)
问题在于第二个面板的宽度,如图所示:
我该如何解决?
因为在流程布局中它看起来像我想要的:
所以我知道下面的方法会导致两个变量都指向同一个对象,但如果我确实想找到最简单的方法让ArrayList s2与s1完全相同,我该怎么办呢?
public static void main(String[] args) {
ArrayList<String> s1 = new ArrayList<String>();
for (int i = 1; i <= 3; i++) {
s1.add(""+i);
}
ArrayList<String> s2 = s1;
}
Run Code Online (Sandbox Code Playgroud) "赞成合成而不是继承"这个短语是否适用于Swing组件?在开始设计UI之前,我想收集一些关于这个主题的专业意见以及哪些代码更易于维护.