我有一个必须执行以下操作的方法:
for (int a01 = 1; a01 <= 25; a01++) {
for (int a02 = a01 + 1; a02 <= 25; a02++) {
for (int a03 = a02 + 1; a03 <= 25; a03++) {
...
System.out.println(a01 + "," + a02 + "," + ... + "," + a015);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想指定嵌套的数量(在上面的例子中,我想要15嵌套).有没有办法在这里使用递归编程?
所以,我碰到一些铸造时困惑我byte来char,平时我会做到这一点:
for (byte b:"ABCDE".getBytes()) {
System.out.println((char)b);
}
Run Code Online (Sandbox Code Playgroud)
哪个会打印出来
A
B
C
D
E
Run Code Online (Sandbox Code Playgroud)
我不小心离开了和+之间(char),b并得到了相同的结果!?
像这样:
for (byte b:"ABCDE".getBytes()) {
System.out.println((char) + b);
}
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?
我基本上在做什么(char)(0x00 + b)?因为
System.out.println((char) - b);
Run Code Online (Sandbox Code Playgroud)
产生不同的结果.
注意:使用Java版本1.8.0_20
我需要以两种不同的方式打印pdf文件 - 通过网页,用户将看到打印预览并选择打印机并进行打印.第二种方法是自动化打印,只需单击一个按钮,pdf就应该发送到打印机.
第一种打印方式是通过网页工作正常,但不是第二种方式.成功检索默认打印机以进行自动打印,但未打印,我也没有收到任何错误.以下是我的分析:
DocFlavor不支持.然后我列出了该DocFlavor打印机的支持,其中一个是application/octet-stream,即DocFlavor.INPUT_STREAM.AUTOSENSE.所以打印机支持这种味道.PrintJobListener以检查打印作业是否失败.当我将该监听器添加到printJob时,如果作业成功,它将打印
No_More_Events并且DATA_TRANSFER_COMPLETE应该打印
JOB_COMPLETE.job.print(),它进入Win32PrintJob.print()方法.我做了F6来执行每一行,看看它在做什么.我将它与GrepCode中的代码进行了比较,因为源代码未在eclipse中加载.一切顺利,我看不出任何错误.它没有进入的唯一地方是它检查mDestination
值的块,因为我没有提供它,它没有通过.请参阅以下代码:
if (mDestination != null) { // if destination attribute is set
try {
FileOutputStream fos = new FileOutputStream(mDestination);
byte [] buffer = new byte[1024];
int cread;
while ((cread = instream.read(buffer, 0, buffer.length)) >= 0) {
fos.write(buffer, 0, cread);
}
fos.flush();
fos.close();
} catch (FileNotFoundException fnfe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw …Run Code Online (Sandbox Code Playgroud) 我的项目中有以下抽象通用数据持有者(简化):
public abstract static class Value<E> {
E value;
public void setValue(E value) {
this.value = value;
}
public E getValue() {
return this.value;
}
public String toString() {
return "[" + value + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
随着一个InputCollection包含列表Objects:
public static class InputCollection {
private ArrayList<Object> values;
public InputCollection() {
this.values = new ArrayList<>();
}
public void addValue(Value<?> value) {
System.out.println("addding " + value + " to collection");
this.values.add(value);
}
public <D> D getValue(Value<D> value, D defaultValue) …Run Code Online (Sandbox Code Playgroud) 给定a Map<String, Object>,值是一个String或另一个Map<String, Object>,如何使用Java 8将地图展平为单个值列表?
例:
Map - "key1" -> "value1"
- "key2" -> "value2"
- "key3" -> Map - "key3.1" -> "value3.1"
- "key3.2" -> "value3.2"
- "key3.3" -> Map - "key3.3.1" -> "value3.3.1"
- "key3.3.2" -> "value3.3.2"
Run Code Online (Sandbox Code Playgroud)
对于上面的例子,我想要以下列表:
value1
value2
value3.1
value3.2
value3.3.1
value3.3.2
Run Code Online (Sandbox Code Playgroud)
我知道它可以这样做:
public static void main(String args[]) throws Exception {
//Map with nested maps with nested maps with nested maps with nested......
Map<String, Object> map = getSomeMapWithNestedMaps();
List<Object> values = …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下结构的枚举:
public enum Friends {
Peter("Peter von Reus", "Engineer"),
Ian("Ian de Villiers", "Developer"),
Sarah("Sarah Roos", "Sandwich-maker");
private String fullName;
private String occupation;
private Person(String fullName, String occupation) {
this.fullName = fullName;
this.occupation = occupation;
}
public String getFullName() {
return this.fullName;
}
public String getOccupation() {
return this.occupation;
}
}
Run Code Online (Sandbox Code Playgroud)
我现在想用来switch确定变量name是否与某个变量相关联enum:
//Get a value from some magical input
String name = ...
switch (name) {
case Friends.Peter.getFullName():
//Do some more magical stuff
...
break; …Run Code Online (Sandbox Code Playgroud) 我需要一些帮助学校的java项目.请参阅下面的代码,了解Book和BookApplication类,以及输入文本文件和所需的输出文本文件.
我的输出文件有问题.当我在Word或写字板或NotePad ++中打开它时,文件格式正确.但是当我在NotePad中打开文件时,报告全部都在一行上.
由于某种原因,NotePad无法识别新的行字符.有什么想法吗?如何更改Java代码以便NotePad正确显示报告?我是一个Java菜鸟,我在这个Java课程中真的很挣扎.您可以提供的任何帮助将非常感激.谢谢.
名为inBook.txt的输入文本文件如下所示:
Java简介0123444555680 Tony Gaddis TG003 Javascript简介9780071632969 John Pollock JP402游戏开发基础9780763778 Heather Maxwell Chandler HC026
名为outBook.txt的输出文本文件需要完全按如下方式读取:
Dean Publishers - 书籍清单
Java简介:Tony Gaddis(TG003)ISBN:0123444555680
Javascript简介作者:John Pollock(JP402)ISBN:9780071632969
游戏开发的基础由Heather Maxwell Chandler(HC026)ISBN:9780763778
我们目前的目录有3本书.
感谢您的关注.
// Book.java
public class Book {
public static final String BOOK_PUBLISHER = "Dean Publishers";
private String bookTitle;
private String bookISBN;
private Author bookAuthor;
Book(String inTitle) {
setBookTitle(inTitle);
setBookISBN("0");
setBookAuthor("");
}
public void setBookTitle(String inTitle) {
bookTitle = inTitle;
}
public void setBookISBN(String inISBN) {
bookISBN = inISBN; …Run Code Online (Sandbox Code Playgroud) 我有一个通用的接口,我想以通用的方式实现这个接口:
public interface BaseBean<T> {
public T create(T t);
public T read(Long id);
public T update(T t);
public void delete(T t);
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我也不能使实现通用.Eclipse以非泛型方式实现所有接口方法:
public class BaseBeanImpl<T> implements NewBaseBean {
@Override
public Object create(Object t) {
return null;
}
@Override
public Object read(Long id) {
return null;
}
@Override
public Object update(Object t) {
return null;
}
@Override
public void delete(Object t) {
}
@Override
public Object find(Long id) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
当我改变时:
public Object create(Object t)
Run Code Online (Sandbox Code Playgroud)
至
public …Run Code Online (Sandbox Code Playgroud) 我有104k字符串值,其中89k是唯一的.我想检查一下该列表中是否存在字符串.
这是我的类及其保存所有这些记录的方法.
public class TestClass {
private static TestClass singletonObj = null;
private List<String> stringList= null;
public static synchronized TestClass getInstance() {
if(singletonObj == null) {
singletonObj = new TestClass();
}
return singletonObj;
}
public boolean isValidString(String token) {
if(stringList == null) {
init();
}
if(stringList != null && token != null && !token.isEmpty())
return stringList.contains(token.toLowerCase());
return false;
}
private init() {
stringList = new ArrayList<String>();
// put all 104k values in this data structure.
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序尝试同时使用此isValidString()方法,每秒约20个请求.这工作正常但是当我尝试将数据结构更改为时 …
我试图在Eclipse Checkstyle插件中创建一个规则,该规则将检查do..while语句的while部分是否与结束括号在同一行.
我的目标是让以下内容有效:
int count = 0;
do {
System.out.println(count);
count++;
} while (count < 10);
Run Code Online (Sandbox Code Playgroud)
以下必须无效:
int count = 0;
do {
System.out.println(count);
count++;
}
while (count < 10);
Run Code Online (Sandbox Code Playgroud)
我试过通过为do关键字创建一个Right Curly Brace放置规则(在Blocks下),指定"same"选项.但是,运行checkstyle会抛出上述} should be on the same line两个示例的错误.
奇怪的是,将选项更改为"单独"会导致两个示例都成功解析.还有其他方法可以执行上述规则吗?
java ×10
eclipse ×2
generics ×2
java-8 ×2
arraylist ×1
casting ×1
checkstyle ×1
enums ×1
flatten ×1
hashset ×1
java-stream ×1
long-integer ×1
performance ×1
printing ×1
recursion ×1
syntax ×1