Ramesh Gupta
Ramesh Gupta

Reputation: 21

Two Dimensional Array not Working correctly

String Hello = "abc";
        rowValue.add(Hello);
        completeData.add(rowValue);
        System.out.println(" One " +completeData);
        rowValue.clear();
        String value= "def";
        rowValue.add(value);
        completeData.add(rowValue);
        System.out.println(" Two " +completeData);

Output: One [[abc]] Two [[def], [def]]...

How to get output as: One [[abc]] TWo [[abc], [def]]

I am not sure, What is happening after Clear() function call.

Upvotes: 0

Views: 32

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Don't call clear. You are adding one reference. Change

rowValue.clear();

to something like

rowValue = new ArrayList<>();

Upvotes: 1

Related Questions