2

I need to add values to an array of objects dynamically and pass that array variable to a function.

If I add values manually it works fine.

Object[][] listData = {{"Cheese", "Pepperoni", "Black Olives"},{"Cheese",    
"Pepperoni", "Black Olives"}};

This is the function that needs to use listData .

TableModel tblModel = new DefaultTableModel(new String[]{"Date", "Action", "Amount"}, listData); 

but how can I add values into listData from a for loop?

ArrayList<Map<String, Object>> sampleArray =   (ArrayList)myPremiumspaid.get("Data"); 
for(int x =0; x<sampleArray.size(); x++)
 {
    //sampleArray.get(x).get("YearMonth");
   //listData[][] = "";  stuck here
 }
12
  • where do you get the values for the array from? Commented May 3, 2016 at 13:02
  • Java arrays have a fixed length; what do you mean "add values ... dynamically"? Commented May 3, 2016 at 13:02
  • What is the type of sampleArray ? Commented May 3, 2016 at 13:04
  • ArrayList<Map<String, Object>> sampleArray Commented May 3, 2016 at 13:07
  • i want to pull certain values from the Map, then populate Object[][], please check my updated question Commented May 3, 2016 at 13:12

1 Answer 1

4

Arrays are fixed sized, after creating the array Object, you can't update it's size/ enlarge it's size. So the purpose to be dynamic size or auto growing sized, you need to use List ie. ArrayList.

Object[][] listData = {{"Cheese", "Pepperoni", "Black Olives"},{"Cheese",    
"Pepperoni", "Black Olives"}};

Instead:

List<List<Object>> listData=new ArrayList<List<Object>>();
listData.add(Arrays.asList("Cheese", "Pepperoni", "Black Olives"));
listData.add(Arrays.asList("Cheese", "Pepperoni", "Black Olives"));

But this ArrayList needs to be passed into a function which is taking array, so, you could convert the ArrayList to array object.

TableModel tblModel = new DefaultTableModel(new String[]{"Date", "Action", "Amount"}, (Object[][]) listData.toArray()); 

but how can I add values into listData from a for loop? Now you could do it as follows:

ArrayList<Map<String, Object>> sampleArray =   (ArrayList)myPremiumspaid.get("Data"); 
for(int x =0; x<sampleArray.size(); x++)
 {
    //sampleArray.get(x).get("YearMonth");
   listData.get(x).add(sampleArray.get(x).get("YearMonth"));
 }

Codename One's DefaultTableModel has an addRow method that accepts an array or series of Objects so just using:

((DefaultTableModel)tblModel).addRow("Cheese", "Pepperoni", "Black Olives"); 

Should work.

Sign up to request clarification or add additional context in comments.

3 Comments

FYI added a small refinement to the answer at the bottom referring to DefaultTableModel in Codename One
I managed to specify the size of listData array from the size of my returned data (sampleArray)
if you define the size of listData by the size of returned data of sampleArray, if you need to add more data from another object, you will not be able to extend the size of array because after creating the array, u cant extend it. So if you need dynamic growable size, you should use ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.