0

I have a really simple task but I'm a beginner and have no idea how do do it. I have to make a method that will take two parameters: an array of Strings, and a word. We are assuming that the array contains a group of words that are already alphabetized in order. I need to take the word and insert it into the array in the correct alphabetical position, and shift all the previous array elements over accordingly. Here is my code so far but I think it's completely wrong...

public static void insertWordIntoArray(String[] arr, String word){
    int i = 0;
    while(arr[i].compareTo(word) > 0){i++;} 
    String temp = ""; String tempV = "";
    temp = arr[i];
    arr[i] = word;

    for (String ind : arr){
        i++;
        if(i<9)tempV = arr[i+1];
        if(i<9)arr[i+1] = temp;
        temp = tempV;
    }

}
mdml
  • 22,442
  • 8
  • 58
  • 66
  • 1
    FYI, If you don't know how to solve it, then it isn't an easy task. – vallentin Nov 30 '13 at 23:36
  • 3
    Add a proper title, and do not SHOUT. – str Nov 30 '13 at 23:36
  • it isn't easy for me but for most people it might be... sorry i'm new – user3053331 Nov 30 '13 at 23:37
  • what should happen to the item that got shifted away? You cannot change the size of an array in java, so you'll need to allocate a new array larger than arr to fit all existing contents of arr and the new word. Or are you fine with discarding that element? – Lie Ryan Nov 30 '13 at 23:47
  • we assume that it is a relatively large array and we don't need to worry about all the space being allocated. – user3053331 Dec 01 '13 at 00:06
  • Given that this is an assignment, have you asked your instructor about it? Also, have you tried using a debugger to see what is going where and why? What doesn't work? –  Dec 01 '13 at 01:10

2 Answers2

1

what do you mean by 9? is it the size of the arr? if it is the size, then this will only work if the elements in the array is less then the array size... I think you will find yourself with array out of bound error if elements is same as array size...

array is dynamic...

i think best not to use number such as 9, because this will make the method is not portable...

Can I increase the size of a statically allocated array? http://en.wikipedia.org/wiki/Dynamic_array

Community
  • 1
  • 1
K.B
  • 11
  • 3
0

yeah, that's an insertion sort. You may be better off using a

LinkedList<String>

, you can insert into it without the need to move the elements.

Kristóf Szalay
  • 1,177
  • 1
  • 8
  • 19
  • that's not an insertion sort, it's a step used in insertion sort. But then maybe I'm just splitting hairs. – Lie Ryan Nov 30 '13 at 23:43
  • you do, but rightfully so :) however, you don't need more steps, just calling this function in a for loop is enough for having a complete IS. – Kristóf Szalay Nov 30 '13 at 23:44
  • I cannot do it like that, for the assignment, I have to make my own method that does it in the way in which I attempted... I cannot use things like Arrays.sort or anything like that. Thanks for the attempt though. PS: we assume that there is enough room at the end – user3053331 Dec 01 '13 at 00:06