0

Java question.

I wanted to print out an array with the to String method, but all I get is this:

fifo.Fifo@2a139a55
fifo.Fifo@15db9742

I know that this points to the point where the arrays are saved, but how can I actually print out the arrays with the toStinr method?

import java.util.ArrayList;

public class Fifo {

    private ArrayList <Object> list;

    public Fifo() {
        this.list = new ArrayList <Object>();
    }

    public void push(Object obj) {
        list.add(obj);
    }

    public ArrayList getList() {
        return this.list;
    }

    public Object pull() {
        if (list.isEmpty()) {
            System.out.println("leer");
            return null;
        }
        Object o = list.get(0);
        list.remove(0);
        return o;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (o.getClass() == this.getClass()) {
            Fifo other = (Fifo) o;
            ArrayList otherList = other.getList();

            if (otherList.size() == this.getList().size()) {
                boolean sameObjects = true;
                for (int i = 0; i < list.size(); i++) {
                    if (!list.get(i).equals(otherList.get(i))) {
                        sameObjects = false;
                    }
                }
                if (sameObjects)
                    return true;
            }
        }
        return false;
    }

    public Fifo clone() {
        Fifo cloneObj = new Fifo();

        for (int i = 0; i < this.list.size(); i++) {
            cloneObj.push(this.list.get(i));
        }
        return cloneObj;
    }
}

This is the seperate method for testing:

import java.util.*;

public class Aufgabe {

    public static void main(String [] args){
        Fifo test = new Fifo();
        Fifo test2;

        test.push(1234);
        test.push("Hallo");
        test.push(5678);
        test.push("du da");

        test.pull();

        System.out.println(test.toString());
        test2=test.clone();
        System.out.println(test2.toString());
        System.out.println(test2.equals(test));
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
marvin
  • 43
  • 1
  • 6

6 Answers6

2

Firstly, you're not using an array, you're using an ArrayList - this is an implemention of List in Java.

Secondly, you're not printing out the array list, you're printing the object containing it - an instance of Foo.

Fifo test = new Fifo();
// do stuff
System.out.println(test.toString());

If you just want to print out the list, you need to use

Fifo test = new Fifo();
// do stuff
System.out.println(test.getList());

A nicer solution would be to override toString in your Foo class.

public class Foo {
  // everything you already have

  public String toString() {
    // you can format this however you want
    return "Contents of my list: " + list;
  }
}

This method will then be automatically called when you pass the object to System.out.println.

Foo test = new Foo();
// do stuff
System.out.println(test);

will result in Contents of my list: [a, b, c] (where a, b, c is actually whatever you have in your list).

Supplementary info When you're using System.out.println in Java, remember:

  • primitives will be printed as they are, e.g. System.out.println(1) will print 1 and System.out.println(false) will print false.
  • Object instances (i.e. non-primitives) will have the toString() method called by println.
  • The default toString of an object is className@hashCode.
  • The default toString of a one-dimensional array is [LclassName@hashCode. Additional dimensions will result in an additional [ at the begining of the string.
Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
1

Arrays in Java are objects which do not override the toString() method, so, as you noted, you'll get the default implementation which contains the class name and the [default] hashCode(). To "properly" convert an array to a string, you could use Arrays.toString:

MyObject[] array = ...;
System.out.println(Arrays.toString(array));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Use this :

Arrays.toString(yourArray);

What toString will print you for an array will contain the hashcode, type of the elements of the array so not really what you are looking for

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

override the Object.toString() and give your implementation that will work fine.

Deepak sai
  • 79
  • 6
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Phani Oct 27 '15 at 12:32
  • Thanks, I am new to this forum and I am understanding bit by bit.. – Deepak sai Oct 27 '15 at 12:47
  • @Phani actually that is (at least part of) the correct answer, so a comment would be inappropriate – Vogel612 Oct 27 '15 at 15:56
0

As others mentioned use Arrays.toString() method.

Coming to

fifo.Fifo@2a139a55
fifo.Fifo@15db9742

This is how toString() method defined in Object class works. It prints a string of the following format:

ClassName@HexadecimalOfHashCode

This is the default implementation defined in the Object class. Since your FIFO class does not override toString() method, invocation of toString() on your FIFO objects, calls the toString() method in Object class.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
0

You need to override toString() in your Fifo class to print something useful. Perhaps something like this would be suitable:

@Override
public String toString() {
    return "Fifo" + list.toString();
}

(This will give you something like Fifo[element1, element2].)

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152