File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes problems creating a new larger array with one extra element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problems creating a new larger array with one extra element" Watch "problems creating a new larger array with one extra element" New topic
Author

problems creating a new larger array with one extra element

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I am creating a program to have information entered into it and will store that into a csv file. I figure if I set rowSize variable at the top of the class so it covers the scope of the program I should be able to call a method to increment rowSize of the array and insert the new element into the new part of the array created. The program runs smoothly if I don't try and add the code to increment the rowSize and w (lines 144 & 145) but when they're run I get index out of bounds exceptions at line 150. What I'm I doing wrong to create this growing CSV file?

With the Error/output

Sorry for the length.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Arrays in Java have a fixed size. If you want to resize an array you need to create a new one the size you want, and copy all the elements across to it.

Or, a much better solution: use a more appropriate data structure. You want a List (either an ArrayList or a LinkedList), which is designed to allow the size to change.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
That seems like it's going to be a lot of rework. Is there a way that I can convert the arrays I already have to an arraylist?
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I bet I could do something like this.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

Dustin Schreader wrote:I bet I could do something like this.
Possibly, but it seems like an awful lot of work to cover up for the fact that you've made a bad decision to start with.
My advice: follow Matthew's.

Is there a way that I can convert the arrays I already have to an arraylist?
Yes, but it doesn't alter the fact that you're better off with an ArrayList to begin with.
Sometimes the school of hard knocks is the best way to learn, and the lesson here is:
unless you know how many items you're going to be creating, always use a collection rather than an array.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Alright I'll get working on it. Thank you much for the help!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

If you're re-working it ...

...there's another big improvement you could make. You're currently using an array of arrays to represent your rows. What I'd suggest is to create a class representing a single row (called Patient - or whatever makes sense in your application). Give that class fields according to what the columns mean. Then you can use an ArrayList<Patient> to hold them in. It will make your code far more readable and maintainable, which is always a good idea.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
It sounds pretty cool but I'm pretty overworked and tired would you be able to provide me with a small sample so I can understand a little better. I guess I don't understand creating a whole new class for just patient information formatting.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

Dustin Schreader wrote:I guess I don't understand creating a whole new class for just patient information formatting.

Well, something likemight be a start.

However, I suspect that the real problem you're having is that you're spending too much time coding, and not enough thinking about the problem.

You're also not doing yourself any favours with your cryptic codes and field names. What is "pt" for example?

Winston
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
So since I had a an array of arrays previously and now I want to use an arrayList it seems easy enough to add more and more elements to an ArrayList but how do I sort through the list like i did when I was using a for loop to sort through the array of arrays? It seems like I'd be sorting through each entire string of combined variables created by my patient class when, let's say I just wanted to compare against the code of the pt to do a search on them not the code, name, physician, floor, and time.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

Dustin Schreader wrote:It seems like I'd be sorting through each entire string of combined variables created by my patient class when, let's say I just wanted to compare against the code of the pt to do a search on them not the code, name, physician, floor, and time.

I suspect you need to go through the tutorials for object order and sorting. You should also look at java.lang.Comparable and java.util.Comparator.

Winston
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Thank you I will take a look.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Beware of long lines in code tags; I have split some of your output into multiple lines so they will fit onto the width of my screen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems creating a new larger array with one extra element