• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding Array to a List - Cannot Resoleve the Problem for a Week

 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a simple program. I am trying to create a List. Each element of the List is an Array.

When I iterate through this List (recursiveTextArray), I expect to have:

[SSL-389] It is a sunny day
[CFO-137] It is a rainny day

But, I got [CFO-137} It is a rainny day repetitively till JVM memory is exhausted. What is the correct way to add Array to a List?
[ November 13, 2006: Message edited by: Natalie Kopple ]
 
Mark Van Tuyl
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to use an Iterator, that's fine.
If you're going to use a for loop to step through the List, that's also fine.

The problem is that you're trying to combine the two. Your while loop executes until the Iterator has no more elements, but you're never advancing the Iterator to the next element.

Also, your index within the for loop is set to either 0 or 1. You probably want to use i rather than 1.
[ November 09, 2006: Message edited by: Mark Van Tuyl ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two things wrong here. First, you iterate forever because you don't call "next()" on that iterator; the way you typically use an iterator looks like



Now the other problem, about how all the values are the same, happens because you only ever create one array object. When you add something to a list, it's a reference to the object that is added, not a copy of the object itself. Therefore here you have one list with two references to the single array object. You need to use "new" to make a new array each time you want to add something to the list.
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for pointing out the problems in the Java program that I posted. That little test program is now running without any problem.

What I am really trying to do is the Java class shown below. I am still struggling with it. Somehow, the data are eithere not properly added to the List (called the recursiveTextArray) or I still do not iterate the List correctly because when I iterate the List in the static void main(...) method, I see the first line of my data only in the console - [] Animals.

No iteration further.

I am supposed to see:

[] Animals
[OO-3.1} Dog
[TgtObj-3.1] Barks
[OO-3.2] Cat
[TgtObj-3.2] Hacks up furball


Here is the code:
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to better identify the place where the problem happens. But, I have no idea what is the proper Java code to address the problem.

I used the System.out.println to write out the "size" of the List - recursiveTextArray that I am trying to create. The "size' is always 1. Therefore, I am not adding the Array one at a time to the List.

How do I adde Array one at a time to a List?

Here is the simplified code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic