Skip to content

Weird array issue

Chris Zeigler

Asked Aug 17, 2010, 9:49 AM EDT1 answer
Share
LinkedInX
Hey all, I've got something weird going on with an array.  The code is posted below.  This code works just fine in of itself, so I pasted the exact code into a part of another program I was working on.  The first loop will run through just fine and print every element in the array.  The issue is in the second loop, the entire array seems to have been somehow set to the last element.  Again, this exact same code worked in another place, but pasting it into this program and not calling anything else seems to break it.  Any help?

Java
		IADoc[] IADocs = new IADoc[recordAmount];
                int i = 0;


                while (myReader.Read())
                {
                    fileNumber = myReader["itemnum"].ToString();
                    content = myReader["itemtypenum"].ToString();
                    metadata = myReader["itemname"].ToString();
                    IADocs[i] = new IADoc(fileNumber, content, metadata);
                    Console.WriteLine(IADocs[i].toString());
                    i++;
                }


                for (int j = 0; j < IADocs.Length; j++)
                {
                    Console.WriteLine(IADocs[j].toString());
                }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Okay, the issue isn't with the second loop, but the first.  For some reason in the while loop, it's setting every element of the array to the same thing.  I created a subloop within the while loop to print all element of the array and it seems to be setting them all despite the code (as far as I can tell) telling it otherwise...
    0