This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Adding Array to a List - Cannot Resoleve the Problem for a Week Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding Array to a List - Cannot Resoleve the Problem for a Week" Watch "Adding Array to a List - Cannot Resoleve the Problem for a Week" New topic
Author

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

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
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

Joined: Mar 22, 2002
Posts: 60
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 ]

<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
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

Joined: May 06, 2003
Posts: 325
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding Array to a List - Cannot Resoleve the Problem for a Week
 
Similar Threads
Reading strings from a line problem _ any help!!
How to change an integer search method to look for strings - any help
Excel Writing Using Java
Problem With 'static' When Running Stand Alone Java Class
Generating all the permutations nad combinations of the given alphabets