-1

My code array. How to insert item in array?

int listElemCount = jCBWorkerMen.getItemCount();
Object[] selectionValues = null;
for (int i = 0; i < listElemCount; i++) {
        selectionValues[i]  =  (Object[]) jCBWorkerMen.getItemAt(i);
        System.out.println(selectionValues);
     }
String initialSelection = "Dogs";
Object selection = JOptionPane.showInputDialog(null, "What are your favorite   animals?", "Zoo Quiz", JOptionPane.QUESTION_MESSAGE, null, selectionValues,  initialSelection);
System.out.println(selection);
Arslanali
  • 1
  • 7

3 Answers3

0

like below

public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);

        Object[] selectionValues = new Object[list.size()];
        for (int i = 0 ; i < list.size() ; i++) {
            selectionValues[i] = list.get(i);
        }

        for (int i = 0 ; i < selectionValues.length ; i++) {
            System.out.println(selectionValues[i]);
        }
    }
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

Your algorithm is incorrect, this

int listElemCount = jCBWorkerMen.getItemCount();
Object[] selectionValues = null;
for (int i = 0; i < listElemCount; i++) {
   selectionValues[i]  =  (Object[]) jCBWorkerMen.getItemAt(i);
   System.out.println(selectionValues);
}

needs to start by initializing the array. Also, you probably shouldn't print the array until you finish initializing it. Finally, you need to use Arrays.toString(Object[]) because Java arrays don't override toString(). So something like,

int listElemCount = jCBWorkerMen.getItemCount();
Object[] selectionValues = new Object[listElemCount];
for (int i = 0; i < listElemCount; i++) {
    selectionValues[i] = jCBWorkerMen.getItemAt(i);
}
System.out.println(Arrays.toString(selectionValues));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The change code should be like as below

int listElemCount = jCBWorkerMen.getItemCount();
    Object[] selectionValues = new Object[listElemCount];
    for (int i = 0; i < listElemCount; i++) {
            selectionValues[i]  = jCBWorkerMen.getItemAt(i);
            //System.out.println(selectionValues);
         }
   //to view the all the items in the array
   for (int i = 0; i < listElemCount; i++) {            
            System.out.println(selectionValues[i]);
   }
    String initialSelection = "Dogs";
    Object selection = JOptionPane.showInputDialog(null, "What are your favorite   animals?", "Zoo Quiz", JOptionPane.QUESTION_MESSAGE, null, selectionValues,  initialSelection);
    System.out.println(selection);
tarun singh
  • 67
  • 1
  • 6