0

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.

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16
Itzik.B
  • 1,023
  • 2
  • 15
  • 35
  • 1
    What do you suppose happens once you get into the range of i that is twice the size of the actual array size? Aren't you getting an array index out of bounds exception? – Lotsa May 27 '20 at 14:53
  • Read [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. – Code-Apprentice May 27 '20 at 15:14

2 Answers2

1

You are iterating i < cards.length*2 which causes an ArrayIndexOutOfBoundsException on i+=2 whereas the condition should be i < pictures.length. Try changing the condition as below in the loop and it should work fine.

 for (int i = 0; i < pictures.length; i++) {
      cards[2*i] = new Card(pictures[i]);
      cards[2*i + 1] = new Card(pictures[i]);
      Log.i("case", cards[2*i].getPic()+" "+String.valueOf(i));
      Log.i("case", cards[2*i+1].getPic()+" "+String.valueOf(i+1));
}

Also, make sure your logcat has the configuration as this to be able to see the exception when the app crashes.

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16
1

You are iterating with wrong length which is causing IndexOutOfBoundsException,so app is crashing.

for (int i = 0; i < cards.length; 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+1].getPic()+" "+String.valueOf(i+1));
    }

And also in the last Log.i line you were printing the value of cards[i] which gives the same value,but its wrong it was only printing the value of 0,2,4,6,8,10. It should print the value of 0,1,2,3,4,5,6,7,8,9,10,11.

  • 2
    I feel `i < pictures.length` more readable than i+=2 and then taking i/2 to get the picture – Sairaj Sawant May 27 '20 at 15:19
  • @SairajSawant , yes it is more readable but the thing is we are going to add values in card , so its better to iterate with cards.length, Both are correct though in his requirement. – Bimal Kafle May 27 '20 at 15:27