Reputation: 167
I have the following code in java, which takes in input from the user. It is basically a simple database system.
ArrayList<String> commands = new ArrayList<String>();
ArrayList<ArrayList<String>> blocks = new ArrayList<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
System.out.println("Enter the transaction commands.\n");
Scanner scan = new Scanner(System.in);
while(!(line = scan.nextLine()).toLowerCase().equals("end"))
{
commands.add(line);
}
for(String com : commands)
{
String split[] = com.split(" ");
if(!split[0].toLowerCase().equals("get") && !split[0].toLowerCase().equals("numequalto") && !split[0].toLowerCase().equals("rollback") && !split[0].toLowerCase().equals("commit"))
{
if(split[0].toLowerCase().equals("begin"))
{
if(!list.isEmpty())
{
blocks.add(list);
System.out.println(blocks.get(0));
list.clear();
}
else
{
continue;
}
}
else
{
list.add(com);
continue;
}
}
}
System.out.println(blocks.get(0));
The input I give for this program is:
set a 10
set b 20
begin
get a
get b
end
While the expected output is:
[set a 10, set b 20]
[set a 10, set b 20]
I get the output as:
[set a 10, set b 20]
[]
The problem seems to be that the value of the ArrayList> blocks, seems to be overwritten. The last print statement prints the value as an empty ArrayList. I cannot find the exact source of error. Any help in finding out the error will be greatly appreciated.
Upvotes: 0
Views: 215
Reputation: 35008
I believe the following code is the culprit:
blocks.add(list);
System.out.println(blocks.get(0));
list.clear();
You add the list to blocks. Note, when you are adding your list object to blocks, you are not copying the list.
So when you clear the list, it clears the list object that is also referenced in the blocks list.
To avoid this you could just:
blocks.add(list);
System.out.println(blocks.get(0));
list = new ArrayList<String>();
This will create a new list object and leave the one in your blocks list untouched.
Upvotes: 3
Reputation: 1549
This giving correct answer as you expected.
remove that list.clear()
statement
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList<String> commands = new ArrayList<String>();
ArrayList<ArrayList<String>> blocks = new ArrayList<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
System.out.println("Enter the transaction commands.\n");
String line;
Scanner scan = new Scanner(System.in);
while (!(line = scan.nextLine()).toLowerCase().equals("end")) {
commands.add(line);
}
for (String com : commands) {
String split[] = com.split(" ");
if (!split[0].toLowerCase().equals("get")
&& !split[0].toLowerCase().equals("numequalto")
&& !split[0].toLowerCase().equals("rollback")
&& !split[0].toLowerCase().equals("commit")) {
if (split[0].toLowerCase().equals("begin")) {
if (!list.isEmpty()) {
blocks.add(list);
System.out.println("list :" + blocks.get(0));
// list.clear();
} else {
continue;
}
} else {
list.add(com);
continue;
}
}
}
System.out.println("output :" + blocks.get(0));
}
}
Answer i got is
list :[set a 10, set b 20]
output :[set a 10, set b 20]
Upvotes: 1
Reputation: 21961
Your second output is got from last line of System.out.println(blocks.get(0));
. Have a look at your code, you add blocks.add(list);
after a while you clear
the list
. As List
is mutable, so blocks
List
is empty. So, your second output print nothing.
blocks.add(list); //list has added here with values
System.out.println(blocks.get(0));
list.clear(); // list here without values.
Upvotes: 3