It might be a basic question , but please help me. I'm writing code for a tic-tac-toe. I wanted to use a hashmap to document the state of each box.
public class tictactoe extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tictactoe);
}
boolean player = true;
Map<Integer , Integer> hashMaps = new HashMap<>();
{hashMaps.put(233, 333);}
public void thismeth(View view) {
ImageView ig = (ImageView) view;
ig.getId();
if(player){ig.setImageResource(R.drawable.cross);
player = false;
ig.setEnabled(false);}
else{
ig.setImageResource(R.drawable.zero1);
player = true;
ig.setEnabled(false);
}
}
}
The above code snippet does not throw any errors in the hashmap
public class tictactoe extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tictactoe);
}
boolean player = true;
Map<Integer , Integer> hashMaps = new HashMap<>();
hashMaps.put(233, 333);
public void thismeth(View view) {
ImageView ig = (ImageView) view;
ig.getId();
if(player){ig.setImageResource(R.drawable.cross);
player = false;
ig.setEnabled(false);}
else{
ig.setImageResource(R.drawable.zero1);
player = true;
ig.setEnabled(false);
}
}
}
While this code shows put() as an invalid method. Without curly brackets it does not work, but with the curly brackets it does. Can someone please explain why it works with the curl brackets?