0

I have two arrays, one called words, the other called data. I have trouble shifting the strings from data into words. so far I have

    public String[] shiftRightX(String[] words, String[] data)
    {

        for(int i= words.length - 1; i>0; i--)
        {
            words[i]=words[i-1];
            for (int x = 0; x < data.length; x++)
            {
                words [0] = data[x];
            }
        }  
        return words;  
    }

it should result for example in this:

    shiftRightX({"1", "2", "3"}, {"1", "2"}) → {"2", "1", "1"}
    shiftRightX({"1", "2", "3"}, {"1"}) → {"1", "1", "2"}
    shiftRightX({"1", "2"}, {"1", "2"}) → {"2", "1"}

however, it is shifting one extra time at the end.

5
  • 2
    Shifting strings from data into words? Do you mean copying? Commented Jan 1, 2013 at 22:35
  • Can you define what is shifting? It's not clear what result you want to get. Commented Jan 1, 2013 at 22:38
  • Another comment: Why the x-loop? Why not just "words[0] = data[data.length-1]"? Commented Jan 1, 2013 at 22:38
  • I think he's trying to prepend values from one array to the other? Like php's unshift() function. Commented Jan 1, 2013 at 22:39
  • pretty much what it should do is shift everything in array words to the right, replace words [0] with data at [0] and shift again and replace words [0] with data [1] etc. until everything from data has been shifted into words. Commented Jan 1, 2013 at 22:39

3 Answers 3

1

A faster version:

public String[] shiftRightX(String[] words, String[] data)
{
  if (data.length < words.length)
     System.arraycopy(words, 0, words, data.length, words.length - data.length);
  for (int i = Math.max(0, data.length - words.length); i < data.length; i++)
     words[data.length - i - 1] = data[i];
  return words;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to swap loops:

public String[] shiftRightX(String[] words, String[] data)
{
    for (int x = 0; x < data.length; x++)
    {
        for(int i= words.length - 1; i>0; i--)
        {
            words[i]=words[i-1];
        } 
        words[0] = data[x];
    } 
    return words;  
}

But this algorithm can be improved. Now it's complexity is O(n*m) but it can be improved to O(n + m) if shift elements in words array to data.length position instead of 1. But you need to be more careful in this case, because you can get ArrayOutOfBoundException.

Comments

0

I think what you're trying to do is something like this:

 public String[] shiftRightX(final String[] words, final String[] data){
        String[] result = new String[words.length];
        int i = 0;
        for(String str : data){
            result[i] = str;
            i++;
        }
        for(int j=0;i<words.length;j++){
            result[i] = words[j];
            i++;
        }
        return result;
    }

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.