我有一个类定义如下:
public class Person {
private String name;
// constructor and getter/setter omitted
}
Run Code Online (Sandbox Code Playgroud)
我试图打印我的班级实例:
System.out.println(myPerson);
Run Code Online (Sandbox Code Playgroud)
但我得到了以下输出:com.foo.Person@2f92e0f4.
当我尝试打印一个Person对象数组时发生了类似的事情:
Person[] people = //...
System.out.println(people);
Run Code Online (Sandbox Code Playgroud)
我得到了输出: [Lcom.foo.Person;@28a418fc
这个输出是什么意思?如何更改此输出以使其包含我的人名?我如何打印我的对象集合?
注意:这是关于此主题的规范问答.
我用了这段代码。我很困惑为什么此int数组未转换为对象vararg参数:
class MyClass {
static void print(Object... obj) {
System.out.println("Object…: " + obj[0]);
}
public static void main(String[] args) {
int[] array = new int[] {9, 1, 1};
print(array);
System.out.println(array instanceof Object);
}
}
Run Code Online (Sandbox Code Playgroud)
我期望输出:
Object…: 9
true
Run Code Online (Sandbox Code Playgroud)
但它给出:
Object…: [I@140e19d
true
Run Code Online (Sandbox Code Playgroud) 我继承了Exception类来创建自己的异常.类似下面的代码
public class ApplicationException extends Exception {
ApplicationException(String errorMessage) {
super(errorMessage);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是堆栈跟踪始终为空.以下代码将不会向控制台写入任何内容.
ApplicationException(String errorMessage) {
super(errorMessage);
System.out.println(this.getStackTrace());
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它是空的,因为它Throwable(String)正在呼唤fillInStackTrace method.有没有办法填写Stack,还是我应该做的其他事情?
我无法弄清楚为什么在String上调用两次getBytes()会返回两个不相等的字节数组():
final String aString = "hello world";
System.out.println(aString.getBytes());
System.out.println(aString.getBytes());
System.out.println(aString.getBytes());
Run Code Online (Sandbox Code Playgroud)
打印:
[B@59887d29
[B@fd13cab
[B@71e606a9
Run Code Online (Sandbox Code Playgroud)
例如,以下断言总是失败:
Assert.assertEquals(aString.getBytes(), aString.getBytes());
Run Code Online (Sandbox Code Playgroud)
从文档中,我没想到任何不确定性!我错过了什么?
转换回String时,结果是预期的结果,所以我最好的猜测是一些未初始化的填充位?
即以下断言总是通过:
Assert.assertEquals(new String(aString.getBytes()), new String(aString.getBytes()));
Run Code Online (Sandbox Code Playgroud) /*
* Given an array of positive ints, return a new array of length "count" containing the
* first even numbers
* from the original array. The original array will contain at least "count" even numbers.
*/
public class StringEx
{
public static void main(String[] args)
{
int[] nums = {2,3,5,6,8};
int count = 2;
StringEx s1 = new StringEx();
System.out.println(s1.copyEvens(nums, count));
}
public int[] copyEvens(int[] nums, int count)
{
int[] n=new int[count];
int c=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]%2==0&&c!=count)
{ …Run Code Online (Sandbox Code Playgroud) 我必须从外部文件中读取一个数字列表,并将它们插入到自己的数组中,以获得正面或负面的数据.扫描程序读取数字就好了,但是当它将它们插入数组时就会出错.正如您在下面看到的那样,我的代码和输出.文件中的内容是打印在输出中的,但是当我要求它打印数组时,就是乱七八糟的字母/数字/符号.任何人都可以帮我修复它吗?
public class Numbers {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(new FileInputStream("First.dat"));
int Positive[] = new int[20];
int Negative[] = new int[20];
int X = 0;
int Y = 0;
while (reader.hasNextInt()) {
int Start = reader.nextInt();
System.out.println(Start);
if (Start < 0) {
Negative[X] = Start;
X += 1;
}
if (Start > 0) {
Positive[Y] = Start;
Y += 1;
}
}
System.out.println(Positive + " " + Negative);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
3 …Run Code Online (Sandbox Code Playgroud) 我有一个大问题.我只是尝试搜索解决方案,但我没有找到任何解决方案.我有这个代码
public static String getString() throws IOException {
String content = null;
File folder = new File("C:\\Soluzioni.txt");
content = FileUtils.readFileToString(folder) + "\n";
String remainingString = content.substring(content.indexOf("["),
content.lastIndexOf("]") + 1);
System.out.println(remainingString);
return remainingString;
}
Run Code Online (Sandbox Code Playgroud)
这没关系.(为清楚起见)
OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44]
Run Code Online (Sandbox Code Playgroud)
我现在的问题是:
public static String[] arg() throws IOException {
String[] strArray = { getString() };
System.out.println(strArray);
return strArray;
}
Run Code Online (Sandbox Code Playgroud)
当我打印strArray时,我有一个错误(ECLIPSE告诉我这个:[Ljava.lang.String; @ 796686c8).我需要我的字符串(remainingString)成为一个字符串数组(strArray),但它保持相同的格式,也就是说,它始终
OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44]
是数组格式.非常感谢你!