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.

Answers


  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]);


Need Your Help

Best way for read and write a text file

delphi file-io text-files lazarus fpc

I am using the latest version of Lazarus IDE and I have a Memo1 on my TForm1. I have to load a text file in Memo1 and then edit every line of the Memo (I use Memo1.Lines.Strings[i] := ...). At the ...

weapon_term helper of weapon system on "thirdperson" target (cryengine)

cryengine

I just walked through the "SCAR.xml" weapon item example, it's fantastic easy to use as a reference for making a new weapon. There is a question confusing me about block.