I want to initilize an array of object from array of Strings.
I have an array that have 6 String records, and I want to initilize two object with the same String record which mean in the array objects will have 12 records.
for some reason I manage to instance the array of objects, but after the loop end the application crash.
public class Card {
private String pic;
public Card(String pic) {
this.pic = pic;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
@Override
public String toString() {
return "Card{" +
"pic='" + pic + '\'' +
'}';
}
}
END OF Card File.
package com.example.memorygame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private void main() {
String[] pictures = {"crocodile", "duck", "lizard", "mosqitue", "parrot", "zebra"};
//String[] pictures = {"crocodile", "crocodile", "duck","duck", "lizard","lizard", "mosqitue","mosqitue", "parrot","parrot", "zebra" ,"zebra"};
Card[] cards = new Card[pictures.length*2];
Functions functions = new Functions();
for (int i = 0; i < cards.length*2; i+=2) {
cards[i] = new Card(pictures[i/2]);
cards[i + 1] = new Card(pictures[i/2]);
Log.i("case", cards[i].getPic()+" "+String.valueOf(i));
Log.i("case", cards[i].getPic()+" "+String.valueOf(i+1));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main();
}
}
This is the Console logs which indicate the array is fully stacked, and then the application crash.
2020-05-27 17:36:37.345 358-358/? I/case: crocodile 0
2020-05-27 17:36:37.345 358-358/? I/case: crocodile 1
2020-05-27 17:36:37.345 358-358/? I/case: duck 2
2020-05-27 17:36:37.345 358-358/? I/case: duck 3
2020-05-27 17:36:37.345 358-358/? I/case: lizard 4
2020-05-27 17:36:37.345 358-358/? I/case: lizard 5
2020-05-27 17:36:37.345 358-358/? I/case: mosqitue 6
2020-05-27 17:36:37.345 358-358/? I/case: mosqitue 7
2020-05-27 17:36:37.345 358-358/? I/case: parrot 8
2020-05-27 17:36:37.345 358-358/? I/case: parrot 9
2020-05-27 17:36:37.345 358-358/? I/case: zebra 10
2020-05-27 17:36:37.345 358-358/? I/case: zebra 11
I don't have anything after those logs, and i dont find any reason why the application crash.