0

As the code shows field os an empty array that I want to populate with the value l from another method I used arraycopy method and set the first array to point to the new added value but the output is gibberish .

Output:

[J@7dc5e7b4

I have tried with System.out.println(Arrays.toString(field)); but all it prints out is this [] empty brackets.

import java.util.*;

public class Aufgabe_04_02 {

    static long[] field = new long[]{};
    
    static void add(long l) {
        long[] f1 = new long[field.length + 1];
        System.arraycopy(field, 0, f1, 0, field.length);
        f1[0]= l;
        field= f1;  
    }// end add(longl)
    
    public static void main(String[] args) {
        long[] result = field;
        System.out.println(result);
    }// end main
 
}// end class
maloomeister
  • 2,461
  • 1
  • 12
  • 21
kololo
  • 11
  • 1
  • 2
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – maloomeister Nov 17 '21 at 11:28
  • You initialize `field` as an empty `long[]`. You then assign `long[] result = field`. So `result` now references an empty `long[]` as well. You then print `result`, which then of course prints an empty array. What are you expecting? You might want to call your `add()` method. – maloomeister Nov 17 '21 at 11:36
  • actually i commented out the lines where result is initialized and printed I called field directly using System.out.println(Arrays.toString(field)); and that still prints an empty array. – kololo Nov 17 '21 at 11:41
  • so what you are saying is call add() instead of field, but the goal is to call field and print the value added in add() – kololo Nov 17 '21 at 11:42

1 Answers1

1

In java, print arrays using Arrays.toString(). However, there is more to be fixed here.

You initialize field as an empty long[]. After that, you assign long[] result = field. So result now references an empty long[] as well. You then print result, which then of course prints an empty array.

To see your array change, yo might want to actually call your add() method.

For example:

public static void main(String[] args) {
    add(1);
    System.out.println(Arrays.toString(field));
}

Which then prints:

[1]

However you have an issue in your add() method. If you add more than one value, it always overwrites the value at index 0. Due to this line: f1[0]= l

So, what you might actually want to do is to add the new value at the end of the array, meaning at the index that you just increased by 1.

So you want to change this line of code:

f1[0] = l;

to

f1[field.length] = l;

The "final" snippet:

static long[] field = new long[] {};

static void add(long l) {
    long[] f1 = new long[field.length + 1];
    System.arraycopy(field, 0, f1, 0, field.length);
    f1[field.length] = l;
    field = f1;
}

public static void main(String[] args) {
    add(1);
    System.out.println(Arrays.toString(field));
    add(3);
    System.out.println(Arrays.toString(field));
}

Prints the following output:

[1]
[1, 3]
maloomeister
  • 2,461
  • 1
  • 12
  • 21
  • okay i get it thanks man that was very helpful – kololo Nov 17 '21 at 11:56
  • @kololo you are welcome. If this answers your questions, feel free to accept / vote the answer. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – maloomeister Nov 17 '21 at 11:58