例如:
public String add(Set<?> t){
...;
}
public <T> String add(Set<T> t){
...;
}
Run Code Online (Sandbox Code Playgroud)
第一个使用通配符泛型; 第二种是通用方法的正常形式.有什么不同?
在什么情况下我们需要通配符泛型,而不是普通的泛型?
我正在尝试使用Mockito ArgumentCaptor在我的方法中获取mime消息。当我返回捕获对象时,其值为null。我想调试它,但是Mockito用增强器包装了它,所以我看不到内容。这适用于我的方法中的对象。有人有主意吗?
这是我的样本测试。msg不为null,但此后调用该方法将返回null。
@Test
public void testSendTemplatedMail() throws MessagingException, IOException {
Context ctx = new Context();
ctx.setVariable("name", "John Doe");
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
String templateName = "testEmailTemplateWithoutImage";
when(mailSenderMock.createMimeMessage()).thenReturn(mock(MimeMessage.class));
try {
mailUtils.sendTemplatedMail("John Doe", "john.doe@bbc.com",
"no-reply@leanvelocitylabs.com", "Hello",
templateName, ctx);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
ArgumentCaptor<MimeMessage> msg = ArgumentCaptor.forClass(MimeMessage.class);
verify(mailSenderMock, times(1)).createMimeMessage();
verify(mailSenderMock, times(1)).send(msg.capture());
verifyNoMoreInteractions(mailSenderMock);
System.out.println("Sample msg subject = " + msg);
System.out.println("Sample msg ctype = " + msg.getValue().getContentType());
System.out.println("Sample msg to = " + msg.getValue().getAllRecipients());
System.out.println("Sample …Run Code Online (Sandbox Code Playgroud) 我正在使用JavaFX 8 Pagination控件.
页面导航显示在底部,但我希望它显示在顶部,显示在实际数据之上.
如何在顶部显示页面导航(数字行和箭头按钮)Pagination?
我想写一个程序,我掷两个骰子,直到我得到那些骰子的总和为11.我想记录当我得到11的总和时使用的骰子的"尝试"或掷骰数量.
我的代码到目前为止:
public class Dice {
public static void main(String[] args) {
int counter1 = 0; //Amount of rolls player 1 took to get sum of 9
int P1sum = 0;
do {
int P1Die1 = (int)(Math.random()*6)+1;
int P1Die2 = (int)(Math.random()*6)+1;
P1sum = (P1Die1 + P1Die2);
counter1++;
} while (P1sum == 11);
System.out.println("Player 1 took "+counter1+" amount of rolls to have a sum of 11.");
}
}
Run Code Online (Sandbox Code Playgroud)
它只是保持打印,需要1卷才能获得11的总和,所以有些事情是不对的.
我的目标:让玩家1继续滚动2直到我得到11的总和,并记录它花了多少尝试.然后让玩家2也这样做.然后,任何具有较低尝试次数的玩家"赢得"游戏.
帮助赞赏谢谢我新手
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
c.set(2007, 0, 1);
System.out.println(c.getTime());
Run Code Online (Sandbox Code Playgroud)
输出:
Tue Sep 12 12:36:24 IST 2017
Mon Jan 01 12:36:24 IST 2007
但是,当我在不同的环境中使用相同的代码时,输出更改为:
输出:
Tue Sep 12 12:36:24 IST 2017
2007年1月1日星期一12:36:24 GMT
仅供参考,我尝试在设置值之前和之后打印日历实例的时区,两者都在"IST"中.
我想知道这个的根本原因.
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class arraysAsList {
public static void main(String[] args) {
String [] arrayA = {"Box","Sun","Clock","Phone"};
Integer [] arrayB = {21,27,24,7};
List listStructureA = new ArrayList();
List listStructureB = new ArrayList();
listStructureA = Arrays.asList(arrayA);
listStructureB = Arrays.asList(arrayB);
System.out.println("My first list : " + listStructureA);
System.out.println("Sun = " + listStructureA.get(1));
System.out.println("My second list : " + listStructureB);
System.out.println("24 = " + listStructureB.get(2));
}
}
Run Code Online (Sandbox Code Playgroud)
我知道int是一个原始类型,而Integer是一个类.但是在这个脚本中,当我尝试使用int而不是Integer时,我得到'index out of bounds exception'错误.之前我使用int来创建数组,int数组和Integer数组之间的区别是什么?提前致谢.
这是一个例子:
package com.demo;
public class PassArray {
static void vaTest(int... v){
System.out.println("no of args : "+v.length+"contents: ");
for (int x:v){
System.out.println(x+" ");
}
}
static void vaTest(boolean... v){
System.out.println("no of args : "+v.length+"contents: ");
for (boolean x:v){
System.out.println(x+" ");
}
}
public static void main(String args[]){
vaTest(1,2,3);
vaTest(true,false,true);
vaTest();//Error:Ambiguous!
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我:
我有一些问题
1.为什么会出现模棱两可的错误?
2.i有一个Varargs参数就好
int doIt(int a,int b,int c,int... vals)
Run Code Online (Sandbox Code Playgroud)
为什么必须最后声明varargs?
我正面临SimpleDateFormat的问题.当我试图执行的程序即时得到输出2014年1月1日20时18分十八秒 insted的的2014年10月1日20时18分十八秒.我发布了我的密码.请帮我在代码中发现错误.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ValidDate {
public static void main(String[] args) {
ValidDate vd = new ValidDate();
vd.processDate();
}
private void processDate() {
SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-yyyy HH:mm:ss");
try {
Date dt = sdf.parse("10-01-2014 20:18:18");
System.out.println(sdf.format(dt));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我试图以特定格式获取当前日期,但不是月份得到分钟
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CurrentDate {
public static String currentDate() {
String timeStamp = new SimpleDateFormat("mm/dd/yyyy").format(Calendar.getInstance().getTime());
return timeStamp;
}
public static void main (String [] args) {
System.out.println(currentDate());
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码会给出分钟......我试过mon ... ???
我使用以下条件将当前系统时间作为开始时间和明天时间作为我的跟踪日志项目中的停止时间.但是现在我想要starttime应该是当前系统时间的+ 1分钟.任何人都可以帮忙吗?
SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String startTime=date_format.format(new Date());
String tomorrowTime=date_format.format(new Date(new Date().getTime()+(1000*60*60*24)));
Run Code Online (Sandbox Code Playgroud)
谢谢