1
List<Object> list = new ArrayList<Object>();
List<Object> l = new ArrayList<Object>();
for (int i = 0; i < list.size(); i++) {
    Object[] row = (Object[]) list.get(i);
    l.add(Arrays.toString(row));
    System.out.println("Element " + i + l);
}

According the that code, output is like that

[Air Bükreş, BU, BUR, [email protected], Doküman Paylaşımı, E-posta]

But I need like that seperately

Air Bükreş

BU

BUR

[email protected]

Doküman Paylaşımı

E-posta

I want it without '[' and ']'

7
  • 2
    Then don't use Arrays.toString(). It will comma separate the array and add the [] characters. Commented Jan 7, 2015 at 10:08
  • 2
    Is this a List<Object> or List<Object[]>? Commented Jan 7, 2015 at 10:11
  • 1
    @tobias_k Object[] can be a Object Commented Jan 7, 2015 at 10:13
  • You can write a simple method that prints out row your way. Commented Jan 7, 2015 at 10:13
  • @tobias_k, better to say it could be declared as such for more readability. Commented Jan 7, 2015 at 10:15

4 Answers 4

1

You can define a simple method instead of Arrays.toString(row), that prints out/returns your way

for Object[]

public String printRow(Object[] row) {
    StringBuilder s = new StringBuilder();
    for (Object object : row) {
        System.out.println(object);
        s.append(object + "\n");
    }
    return s.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're question is very unclear, but I believe you are adding an entire array inside a single position of your ArrayList. You probably have list.add(items) somewhere, where items is an Object[] and this will not give any errors as that is also an Object and thus will fit as an item in your list. If you want to add them separately you could use list.addAll(Arrays.asList(items))

Comments

0

It's not at all clear to me what you are trying to achieve in your code. However if you question is "how do I print out a list of strings each on a new line" then here are two answers:

list.stream().forEach(System.out::println);

and

System.out.println(list.stream().collect(Collectors.joining("\n")));

Comments

0
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class testo {
    public static void main(String args[]){
    List<Object> list = new ArrayList<Object>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");

    Iterator<Object> it = list.iterator();
    while(it.hasNext()){

        System.out.println(it.next());
    }
}
}

output:

1
2
3
4
5

2 Comments

Output is [Ljava.lang.Object;@6fdb6262
No,my output is like this .try this code .although its same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.