Results 1 to 2 of 2

Thread: Copy elements of an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    1

    Question Copy elements of an array

    I have 4 arrays (a,b,c,d). If the comparison between elements of c & a =, d should be copied with the value of b. follow?
    the problem I have is that it doesn't copy the values from array b to d. I cannot figure y. Can Sb. help me??
    This is my code: {
    String[] words = {"before", "to", "great", "later", "you",
    "very", "see", "message", "one", "smile"};
    String[] codeW = {"b4", "2", "gr8", "l8r", "u", "V.", "c",
    "Mssg.", "1", ""};
    String[] codedArray;
    String message = "";
    {
    //------------ Ask for input of message in plain English
    message = JOptionPane.showInputDialog( "Please enter message" );
    //------------ Tokenizes input, declares array and fill's with tokens
    StringTokenizer stock = new StringTokenizer(message);
    int count1 = stock.countTokens();
    String [] messArray = new String[count1];
    for (int i=0; i< count1;i++)
    messArray[i] = stock.nextToken();
    //------------ Creates new array to hold Coded Message
    codedArray = new String[count1];
    //------------ Compares tokens against words to be coded (array [words]
    for (int n = 0; n < words.length; n++)
    {
    for (int x = 0; x < messArray.length; x++)
    {
    if (messArray[x].equalsIgnoreCase(words[n]))
    {
    System.arraycopy(codeW, n, codedArray, x, 1);
    System.out.println(codedArray[x]);//To check values of array
    }
    else
    {
    System.arraycopy(messArray, x, codedArray, x, 1);
    }
    }
    }
    for (int y = 0; y < codedArray.length; y++)
    JOptionPane.showMessageDialog(null,"Coded Message\n" + codedArray[y]);
    System.exit(0);
    }
    }
    }

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    To copy elements of an array look at the arraycopy() method within the java.lang.System class

    Here is the signature public static void arraycopy(Object source, int src_position, Object dst, int dst_position, int length);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width