1

I am trying to create an array of a class within a class so that I can have multiple sets of the inner class. However because I cannot create an empty an array in Java, I was wonder what's the best way to set this up. I know I can just define an array that is bigger than I would ever use but I feel that kind of sloppy programming.

Here's the important part of the 2 classes:

public class xmldata {
    String Barcode;
    String First;
    String Last;
    String Phone;
    String Email;

    String md5sum;

    String zipfile;

    picture_data[] pics;

...

public class picture_data {

    static String filename;
    static String directory;

As you can see, I to have an array of picture_data in xmldata. I have seen some stuff using lists but the examples are different and I am not sure I understand how to apply it in my case.

Here's the code I used to try and populate the arrays.

    xmldata data = new xmldata();

    ResultSet pictures=db.query("select * from pictures where barcode=?",barcode);
    int i = -1;
    while (pictures.next()) {
        ++i;
        data.pics[i].setdirectory(pictures.getString("path"));
        data.pics[i].setfilename(pictures.getString("filename"));       
    } 

Any suggestions would be appreciated.

Codeguy007
  • 891
  • 1
  • 12
  • 32

4 Answers4

2

Modern idiomatic Java doesn't use raw Arrays or Vector either, it uses type safe List implementations.

Also picture_data and xmldata are not idiomatic naming convention for classes in Java, it should be PictureData and XMLData. I would challenge the semantics of a class called PictureData or XMLData as well.

A correct solution would be something like

List<PictureData> list = new ArrayList<PictureData>();

Understanding how to work with the Collections framework in Java is a fundamental requirement to be productive. Type safe Lists are a core component to writing real Java code.

  • This is doing something weird. I seem to be able to add to the list but when I pull values back out of the list they are always the last value I put into the list and not the correct values. – Codeguy007 Apr 27 '13 at 19:55
1

If your array's size is going to be dynamic, then use lists inside and an ArrayList precisely. This way, you don't have to take care about size because it's treated internally.

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
1

Create an object of picture_data and add it into a ArrayList of picture_data

Then convert that arraylist into an array

Convert ArrayList<String> to String[] array

http://www.java-tips.org/java-se-tips/java.lang/how-to-convert-an-arraylist-into-an-array.html

Community
  • 1
  • 1
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Best option would be to use lazy initialized ArrayList

public class Xmldata {

    List<picture_data> pics;

    public void addPics(picture_data data) {
       if(pics == null) pics = new ArrayList<picture_data>();
       pics.add(data);
    }
}

Here the pics list will only be created if the picture_data type objects are added to the Xmldata class

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • I'm assuming with this method I would need to create an instance of picture_data, add the data too it, and then add the instance to my XmlData instance. – Codeguy007 Apr 27 '13 at 19:18
  • Yes.. you need to create one instance.. in your `while` loop – sanbhat Apr 27 '13 at 19:20
  • This isn't working. I'm getting multiple entries of the same thing. The values in the list are wrong. – Codeguy007 Apr 27 '13 at 19:45