0

I have a simple solution, but would like to find a more streamlined solution, if I were to update my database.

Right now I am looking for ten floats, using this code I have no problem, these take the ten floats I have stored in my database.

private float[] getRandomData() {
        return new float[] { 63, 82, 202, 41, 322, 123, 53, 178, 126, 1393 };
    }

What I would rather do, is take my database, and add the stored values to the array.

Something along the lines of...

private float[] getPopulation() {
        twcCountryData populationData;

        for (int i =0; i < countryDataList.size(); i++) {
            populationData = countryDataList.get(i);
            populationData.getPopulation();        
        }
        return new float[] {};
    }

I am looking to basically end up with the same outcome as the first codeblock, but iterating through my datalist adding to the new float, allowing for future database additions, to take care of themselves.

  • as I understand, you want a kind of "listener" that would update your java array when a change occurs in the DB? for that, you can look at http://stackoverflow.com/questions/12618915/how-to-implement-a-db-listener-in-java – syllabus Dec 19 '14 at 07:10
  • Not exactly, I have working code that handles my database manager, reading and updating. The populationData.getPopulation holds ten values, the same values that I manually added to the float[]. I am looking for a solution that adds each value to the array, the above code is far from complete, as I am not sure how to iterate through the populationData and update the length and contents of the float[]. – TheBluePotter Dec 19 '14 at 11:08
  • what's the type of the object returned by populationData.getPopulation() ? – syllabus Dec 19 '14 at 11:16
  • The object is a table... CREATE TABLE countries (entryID INTEGER PRIMARY KEY NOT NULL, CountryName TEXT, Capital TEXT, CountryStringName TEXT, CurrencyCode TEXT, Latitude FLOAT, Longitude FLOAT, Continent TEXT, Comment TEXT, Population FLOAT); The last entry, the population, is what I am trying to store in a float[]. – TheBluePotter Dec 19 '14 at 11:20

1 Answers1

2

so the java type of populationData.getPopulation() is float

you can do something like that:

private float[] getPopulation() {
        twcCountryData populationData;

        float[] population = new float[countryDataList.size()];
        for (int i =0; i < countryDataList.size(); i++) {
            populationData = countryDataList.get(i);
            population[i] = populationData.getPopulation();        
        }
        return population;
    }
syllabus
  • 581
  • 3
  • 9