我正在Java上开展一个项目,要求我制作一些"容器"类,如果你愿意的话.这是一个简单的版本:
public class Pair{
Object KEY;
Object VALUE;
public Pair(Object k, Object v)
{
KEY = k;
VALUE = v;
}
public Object getKey()
{ return KEY; }
public Object getValue()
{ return VALUE; }
}
Run Code Online (Sandbox Code Playgroud)
(请注意,这是严格简化的,我在最终版本中使用了正确的set/get方法.)
我的问题是:
使用ArrayList作为参数调用println方法时,例如:
ArrayList<String> arr = new ArrayList<String>();
arr.add("one");
arr.add("two");
System.out.println(arr);
Run Code Online (Sandbox Code Playgroud)
Java自动知道如何正确打印ArrayList的每个"节点"或元素.
有没有办法编写一个方法来允许println方法正确打印我的Pair类?
Java System.out.println(String x)声明的输出是否有任何字符限制?
当我尝试使用Web服务调用打印一些XML时,System.out.println()实际上只有一部分打印在控制台中.
我想要打印的XML字符串很大.
为什么会这样?
这是我的代码:
public static void main(String[] arg)
{
String x = null;
String y = "10";
String z = "20";
System.out.println("This my first out put "+x==null?y:z);
x = "15";
System.out.println("This my second out put "+x==null?y:z);
}
Run Code Online (Sandbox Code Playgroud)
我的输出是:
20
20
Run Code Online (Sandbox Code Playgroud)
但我期待这个:
This my first out put 10
This my second out put 20
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下为什么我得到"20"作为两个println调用的输出?
下面的Scala代码正是我所期望的 - 它打印some_file.txt的每一行.
import scala.io.Source
val lines = Source.fromPath("some_file.txt").mkString
for (line <- lines) print(line)
Run Code Online (Sandbox Code Playgroud)
如果我使用println而不是print,我希望看到some_file.txt以双倍间距打印出来.相反,程序在some_file.txt的每个字符后面打印一个换行符.有人可以向我解释一下吗?我正在使用Scala 2.8.0 Beta 1.
我有一个多线程Java应用程序,它将收到的有关消息的信息输出到控制台以进行调试.每次应用程序收到消息时,它都会调用System.out.println(String)该消息.
我遇到的问题是,如果应用程序充满了消息,则会System.out.println()输出错误的信息(如旧的缓冲区信息).这让我想知道是否存在线程问题,其中多个线程一次调用该println函数,并且没有正确刷新缓冲区.
在我的主程序(线程)中,我有一些效果:
while(iterator.hasNext())
{
SelectionKey key = iterator.next();
channel.receive(buffer); // The buffer is a ByteBuffer.
buffer.flip();
new Thread(new ThreadToPrintTheMessage(buffer)).start();
buffer.clear();
iterator.remove();
}
Run Code Online (Sandbox Code Playgroud)
在我的帖子中,我有一些效果:
@Override
public void run()
{
System.out.println(message);
System.out.flush(); // I have better results with this. But, it doesn't
// fully resolve the issue.
}
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以让多个线程一次打印到控制台而不包含包含旧信息的缓冲区?
谢谢
编辑:更新主线程中的代码,以更好地代表我的程序正在做什么.
//take the input from user
text = br.readLine();
//convert to char array
char ary[] = text.toCharArray();
System.out.println("initial string is:" + text.toCharArray());
System.out.println(text.toCharArray());
Run Code Online (Sandbox Code Playgroud)
输出:
initial string is:[C@5603f377 abcd
我需要在控制台上打印大量数据(大约100 MB).使用println在IntelliJ上惨遭失败.是否有像console.log这样的替代方案可以处理和显示这些数据而不会滞后和减速?
提前致谢!
我创建了这个程序,以帮助我在创建的文本冒险中绘制地图.到目前为止,它所做的只是绘制你输入的黑色方格的拘留.有没有办法去除每条线之间的空间,所以没有白线穿过广场?
这是我的代码:
import java.util.Scanner;
public class MapGrid {
static String createGrid(int x, int y) {
String output = "";
String block = new String("\u2588\u2588"); //A string of two unicode block symbols: ??
if(x * y != 0) { //If neither x nor y is 0, a map of length x and height y will be returned
for(int n = 1; n <= y; n++) {
for(int i = 1; i <= x; i++) {
output += block;
}
output += …Run Code Online (Sandbox Code Playgroud) 它总是返回零.
这不是说它纯粹吗?
请问的println引起任何副作用?
好的,所以我是新手,我一直在寻找两周的时间来尝试将一个SQL结果集打印到由JavaFX Scene Builder制作的TableView上.我可以使用System.out.println打印我的结果集并且数据是正确的,但是当我尝试将它放入List时,我的tableview显示一列为0,另一列为空.我正在尝试打印List(println),但是我得到了对象指针(?)而不是数据,所以我无法看到我是否正确地从ResultSet传递到List.
任何帮助将不胜感激......我想了解这个概念,而不仅仅是得到答案.
这是我的控制器代码:
public class TesterUIController implements Initializable {
@FXML
private TableView<dataClass> Table;
@FXML
private Button TestButton;
@FXML
private TableColumn<dataClass, Integer> colKey;
@FXML
private TableColumn<dataClass, String> colSalesNo;
public static void main (String[] args) {
System.out.println("Main has run");
}
/**
*
* @param fxmlFileLocation
* @param resources
*/
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
TestButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Button Pressed");
colKey.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("Key"));
colSalesNo.setCellValueFactory(new PropertyValueFactory<dataClass, String>("SalesNo"));
Table.getItems().setAll(gobbledyGook());
}
} );
}
/* …Run Code Online (Sandbox Code Playgroud)