0

I am trying to build a string to pass it as an SQL query within the IN statement.

ArrayList<Object[]> arrayList = new ArrayList<Object[]>();

    List<String> strings = new ArrayList<>(arrayList .size());
    for (Object object : arrayList ) {
        strings.add(Objects.toString(object, null));
    }

    System.out.println("askldnlkasdn"+strings);

This still prints out the memory locations instead of the actual string

askldnlkasdn[[Ljava.lang.Object;@7bb11784, [Ljava.lang.Object;@33a10788, [Ljava.lang.Object;@7006c658, [Ljava.lang.Object;@34033bd0, [Ljava.lang.Object;@47fd17e3, [Ljava.lang.Object;@7cdbc5d3, [Ljava.lang.Object;@3aa9e816, [Ljava.lang.Object;@17d99928, [Ljava.lang.Object;@3834d63f, [Ljava.lang.Object;@1ae369b7]

I have also tried out using StringBuilder and StringUtils. But things dont seem to work.

Any inputs as to where the problem is?

2
  • The code you posted is incorrect. The ArrayList is of Object[], yet when you iterate it, it changes into just Object. I assume is a typo? Commented Aug 11, 2016 at 10:19
  • What is the actual type of underlying Object? You may override toString() method in appropriate class to make it work as expected Commented Aug 11, 2016 at 15:44

5 Answers 5

1

you should override method toString in your objects

Sign up to request clarification or add additional context in comments.

Comments

1

You can use an SQL specific java Array.

try (PreparedStatement stmt = connection.prepareStatement("... IN (?) ...")) {
    Object[] elements = ...
    stmt.setArray(1, connection.createArray("TEXT", elements));
    stmt.executeUpdate();
}

Comments

1

The problem you have is that you are implicitly using the toString() method of the Object elements inside your ArrayList. By default, that method returns the class and address of the Object. You should override the toString() method in every class you will use inside the list so it returns what you want it to.

Comments

0

This is new code that may help,

// Data of Array of Object for test the Code
    Object[] a = new Object[1];
    a[0] = "Hello";
    Object[] b = new Object[1];
    b[0] = "Friend";
    Object[] c = new Object[1];
    c[0] = "This is";
    Object[] d = new Object[1];
    d[0] = "Just Test";

    // The Array List of objects and the data entry
    ArrayList<Object[]> arrayList = new ArrayList<Object[]>();
    arrayList.add(a);
    arrayList.add(b);
    arrayList.add(c);
    arrayList.add(d);

    // New List of strings
    List<String> strings = new ArrayList<>(arrayList .size());


    // The Process of adding the data from array list of objects to the strings
    for(int i = 0; i < arrayList.size(); i++){
        strings.add((String) arrayList.get(i)[0]);
    }

    // Just for print the data to console
    for(int i = 0 ; i < strings.size(); i++){
        System.out.println(strings.get(i));
    }

    System.out.println("askldnlkasdn "+strings.get(0));

I hope that solve the problem, if not please inform me, you can use it for more than one dimensional array.

1 Comment

I had thought of looping through the arrayList before. But, was looking for some alternative solutions before. I tried many solutions as proposed by others. I have finally used the looping method to solve the problem.
0

You can just save it as String , like this code

ArrayList<String> arrayList = new ArrayList<>();

    List<String> strings = new ArrayList<>(arrayList .size());
    for (Object object : arrayList ) {
        strings.add(Objects.toString(object, null));
    }

    System.out.println("askldnlkasdn"+strings);

Or you want it Object for specific purpose?

2 Comments

Yes, the use of object was for some specific purpose.
Ok, i will post my new answare

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.