Java, filling with array Object [] [] with data

I am trying to populate an Object[][] array with data from my object class. However, they have problems filling the array. Below I will try to execute the Object[][] data command. currently returned data variable cannot be seen by the method. I tried to remove the method and populate the array where rows in the declaration, but cannot, because there is a for loop.

Am I currently populating the Object[][] array correctly?

  public class CustomersDialog extends javax.swing.JDialog { private CustomerList customers = new CustomerList(); Object rows[][] = getData(); public Object[][] getData() { customers = dataManager.getUserData(); int size = customers.size(); Customer customer = new Customer(); for(int i = 0; i < size; i++) { customer = customers.getCustomerAt(i); Object [][] data = { { Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } }; } return data; } } 

Next, this method of creating an array outside the loop calls the "empty statement of the operator" by the compiler, and it says that "it requires the end of the line ; after the .get ":

  public Object[][] getData() { customers = dataManager.getUserData(); int size = customers.size(); Customer customer; Object [][] data; for(int i = 0; i < size; i++) { customer = customers.getCustomerAt(i); data = { { Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } }; } return data; } 
+4
source share
3 answers

You are not doing it right.

In your code, you declare an INSIDE for loop array, which means that after the loop, the array no longer exists. That is why you cannot return data - it simply does not exist.

More information about the scope and lifetime of variables that you can read there: http://www.c4learn.com/javaprogramming/the-scope-and-lifetime-of-variables-in-java-programming-language/

What you want to do is declare the array outside the loop:

 Object [][] data; for(int i; i < size; i++) { // Filling data array } return data; 

Next, if you want to use an array, you must first initialize it:

 Object [][] data = new Object [size][3]; 

Then you can fill it in a for loop, for example:

 for(int i; i < size; i++) { customer = customers.getCustomerAt(i); data[i][0] = Integer.toString(customer.getCustomerID()); data[i][1] = customer.getfName(); data[i][2] = customer.getlName(); } 
+4
source

You must allocate the array twice. Once for rows and after once in a row.

 Object[][] data = new Object[size]; for(int i = 0; i < size; i++) { customer = customers.getCustomerAt(i); data[i] = new Object[]{ Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() }; } 
+2
source

If you really want to use a 2-dimensional array, you must first determine the size of the array (also if it is an n-dimensional array!)

See this content for an explanation:

http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html

It seems to me that you have only one size parameter for the array, so I would use a regular array instead of 2-dimensional.

I would do something like this:

 public Object[] getData() { customers = dataManager.getUserData(); int size = customers.size(); Object[] result = new Object[size]; Customer customer; for(int i = 0; i < size; i++) { customer = customers.getCustomerAt(i); result[i] = { { Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } }; } return data; } 

If you need to use a 2-dimensional array of objects, you need to determine the size of the second size and fill the array as follows:

 for(int i = 0; i < sizeDimOne; i++) { for(int k = 0; k < sizeDimTwo; k++) { result[i][k] = { { Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } }; } } 

Hope this will be helpful.

+1
source

All Articles