简体   繁体   中英

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" .

Why it is not showing 1, 2, 3, 4, 5, 6 ?

Im very very new to programming and java. So please guide me. Thanks.

  1. Array index starts from 0 . Use int i = 0 in for-loop .
  2. Use values[i] to print the 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.

You need to use the i variable together with the array. As in values[i] instead of only 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() . Just use System.out.print(values[i]) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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