Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Getting A String From An Arraylist

When I use a for loop(like so:)

  StringBuilder string = new StringBuilder();
    for(int i1 = 0; i1 < array.size()){
      string.append(arraylist.get(i).toString());
    } 

I get an outOfBouds crash. I need to read the arraylist object by object, so arraylist.toString() does no good.

any help? Thanks

like image 787
user1232105 Avatar asked Mar 20 '12 02:03

user1232105


People also ask

How do you return an ArrayList to a String in Java?

Methods: Using get() method of ArrayList class. Using toArray() method of ArrayList class. Using copyOf() method of Arrays class.

How do I retrieve an element from an ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.

Is there a toString for ArrayList?

Note: The ArrayList class does not have its own toString() method. Rather it overrides the method from the Object class.

Can we get index from value in ArrayList in Java?

indexOf() in Java. The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.


4 Answers

You need to increment the loop control variable.

for(int i=0; i<arraylist.size();i++){
   string.append(arraylist.get(i).toString());
}

Or

for(Object str:arraylist){
  string.append(str.toString());
} 
like image 77
KV Prajapati Avatar answered Nov 14 '22 22:11

KV Prajapati


You're using i1 in your loop, but you're accessing element i.

This confusion is probably caused by using non-descriptive variable names. For example, I see array and arraylist - are those supposed to be the same?

So the first concern is just some code clean up. If that isn't the exact code, then show us what is. Also note that you can make a code block by indenting it all 4 spaces. Also a good idea to show us what the stack trace is.

Ideally, a small, complete program we can compile that shows us the error would generate the fastest corrective answer. You might even find the problem as you create that small program.

like image 33
corsiKa Avatar answered Nov 14 '22 23:11

corsiKa


So many errors in just three lines of code. Really. Try this:

StringBuilder string = new StringBuilder();
for (int i1 = 0; i1 < arraylist.size(); i1++) {
    string.append(arraylist.get(i1).toString());
}

Or this:

StringBuilder string = new StringBuilder();
for (Object str : arraylist ) {
    string.append(str.toString());
}
like image 41
Óscar López Avatar answered Nov 14 '22 23:11

Óscar López


I'd write it this way:

public static String concat(List<String> array, String separator) {
    StringBuilder builder = new StringBuilder(1024);
    for (String s : array) {
        build.append(s).append(separator);
    }
    return builder.toString();
}

Have you thought about how you'll keep each string separate in the concatenated version? Do want a space between each? This does not look very useful.

like image 35
duffymo Avatar answered Nov 14 '22 21:11

duffymo