1
String[] msgoptions;
String[] finalmsgs3 = finalmsgs2[3].split("RR");
for(i = 1; i < finalmsgs3.length; i++)
{
    msgoptions[i] = finalmsgs3[i];
    Log.e(TAG, "---------------" + msgoptions[i]);
}

I need your help, if you can resolve issues of my code. Actually i'm trying to assign values of an array variable to another array variable. but i can't do that because got some errors. So, Could you any one help me..?

1
  • 1
    What error do you get? Commented Feb 10, 2013 at 11:05

4 Answers 4

3

You need to initialize the array msgoptions before using it, for example:

String[] msgoptions = new String[SIZE];
Sign up to request clarification or add additional context in comments.

Comments

1

Rewrite your code to:

String[] finalmsgs3 = finalmsgs2[3].split("RR");     // switch first two lines
String[] msgoptions = new String[finalmsgs3.length]; // initilize the other array
for(i = 0; i < finalmsgs3.length; i++)               // Array index starts at 0
{
    msgoptions[i] = finalmsgs3[i];
    Log.e(TAG, "---------------" + msgoptions[i]);
}

A better solution would be:

String[] finalmsgs3 = finalmsgs2[3].split("RR");
String[] msgoptions = Arrays.copyOf(finalmsgs3, finalmsgs3.length);

Comments

0

Try that :

String[] msgoptions = = new String[SIZE];;
String[] finalmsgs3 = finalmsgs2[3].split("RR");
int j=0;
for(i = 0; i < finalmsgs3.length; i++)
{
    msgoptions[j] = finalmsgs3[i];
    j++;
    Log.e(TAG, "---------------" + msgoptions[i]);
}

Comments

0

First, on Java, you need to initialize your arrays. Also please note that they are based on 0 indexes.

So you should change your code to something like this:

String[] finalmsgs3 = finalmsgs2[3].split("RR");
String[] msgoptions = new String[finalmsgs3.length];
for(int i = 0; i < finalmsgs3.length; i++)
{
    msgoptions[i] = finalmsgs3[i];
    Log.e(TAG, "---------------" + msgoptions[i]);
}

But to do array copying you can avoid your code for using something more "standard" like java.util.Arrays.copyOf(T[] original, int newLength)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.