-3
public static void main(String[] args){
    ListNode node1 = new ListNode(4);
    ListNode node2 = new ListNode(5);
    ListNode node3 = new ListNode(2);
    ListNode node4 = new ListNode(21);
    ListNode node5 = new ListNode(43);
    ListNode node6 = new ListNode(52);
    ListNode node7 = new ListNode(12);
   }

I want to declare variables sequentially using 'for' or 'while' syntax in JAVA.

In C, I know that there is a way of declaring variables sequentially using MACRO

But, In java, How can I do this?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Daniel kim
  • 158
  • 1
  • 13

1 Answers1

2

You can create an array of Objects like this by using a for loop:

ListNode[] node = new ListNode[7];
for(int i = 0; i<7; i++) {
    //scan a value you want to pass
    node[i] = new ListNode(valueyouwanttopass);
}

or by using a while loop as shown below:

ListNode[] node = new ListNode[7];
int i = 0;
while(i<7) {
    //scan a value you want to pass
    node[i] = new ListNode(valueyouwanttopass);
    i++;
}
burglarhobbit
  • 1,860
  • 2
  • 17
  • 32