简体   繁体   English

正在打印一些垃圾值。 为什么?

[英]Some junk values is printing. Why?

I wrote the following piece of code. 我编写了以下代码。

public class ArrayLessons {

    public static void main(String[] args) {
        int values[] = { 1, 2, 3, 4, 5, 6 };

        for (int i = 1; i < values.length; i++) {
            System.out.println(values);
        }
    }

}

But the output puts some junk values like this "[I@5df9aeda" . 但是输出会放入一些垃圾值,例如"[I@5df9aeda"

Why it is not showing 1, 2, 3, 4, 5, 6 ? 为什么不显示1, 2, 3, 4, 5, 6

Im very very new to programming and java. 我对编程和Java非常陌生。 So please guide me. 所以请指导我。 Thanks. 谢谢。

  1. Array index starts from 0 . 数组索引从0开始。 Use int i = 0 in for-loop . for-loop使用int i = 0
  2. Use values[i] to print the values . 使用values[i]打印这些values

If you just want to print: Try this code also: 如果您只想打印:请尝试以下代码:

System.out.println(Arrays.toString(array));

You cannot direct print array value without giving any index if you want to print its value you must to go through with loop and its indexing like this please see the following example 您不能不给出任何索引就直接打印数组值,如果要打印它的值,您必须使用循环和这样的索引进行处理,请参见以下示例

public class ArrayLessonsDem {
  public static void main(String[] args) {
      int values[] = { 1, 2, 3, 4, 5, 6 };

      for (int i = 0; i < values.length; i++) {
          System.out.println(values[i]);
      }
  }

}

Array indexing starting point is 0 so you always start with 0 please check my for look and System.out.println Statement care fully. 数组索引的起始点是0,因此您始终以0开始,请检查my的外观和System.out.println语句是否完全受关注。

You need to use the i variable together with the array. 您需要将i变量与数组一起使用。 As in values[i] instead of only values . 就像在values[i]而不是仅在values Also, array indexing is zero-based. 同样,数组索引是从零开始的。

Yes, I agree with Mr. jhn. 是的,我同意詹先生。 Also, if you want to print the results in a single line, you cannot use System.out.println() . 另外,如果要单行打印结果,则不能使用System.out.println() Just use System.out.print(values[i]) ; 只需使用System.out.print(values[i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM