List with Int as first argument and String as second (Java)

I’m trying to write a method that returns an int index as the first argument and a string as the second. The method should insert the string into a list by order of the index’s. If the Int is negative or leaves a gap, it should throw IllegalArgumentException. Here’s what I’ve done:

public MitchList() {
    data = new String[2];
    size = 0;
}
public int size() {
    return size;
}
    public void add(int index, String s){
    if (data.length == size) {
        String[] newArray = new String[data.length*2];
        for (int i=0; i < data.length; i++) {
            newArray[i] = data[i];
        }
        data = newArray;
}
    if (index > 0 || index <size){
    throw new IllegalArgumentException();   
    }

    if (data[index] != ""){
        for (int i = 0; i <size; i++){ 
        s = data[index];
        }
    }
    data[index] = s;
    size++;

}

The JUnit test is saying it is failing. It should return a list like:

1, Cat 2, Dog 3, Squirrel

So on..

Any ideas on a solution?

UPDATE

Here’s what I have now.

/**
  <P>{@code java MitchList}</P>
**/
  public class MitchList  {
  private String[] asData;
  private int iSize;
  public static final void main(String[] igno_red)  {
     MitchList ml = new MitchList();
     test(ml, 0, "hello");
     test(ml, 1, "goodbye");
  }
  public static final void test(MitchList m_l, int i_ndex, String s_tring)  {
     m_l.add(i_ndex, s_tring);

     System.out.print(s_tring + ", ");
  }
  public MitchList() {
      asData = new String[2];
      iSize = 2;
  }
  public int iSize() {
      return iSize;
  }

      public void add(int index, String s){
  System.out.println("asData.length=" + asData.length + ", iSize=" + iSize + "");
      if (asData.length == iSize) {
  System.out.println("1");
          String[] newArray = new String[asData.length*2];
          for (int i=0; i < asData.length; i++) {
              newArray[i] = asData[i];
          }
          asData = newArray;
  }
      if (index > 0 || index <iSize){
  System.out.println("2");
 throw new IllegalArgumentException("index (" + index + ") must be greater than zero    and less than size (" + iSize + ")"); 

       }
  System.out.println("asData[" + index + "]=" + asData[index] + "");

      if (asData[index] != ""){
  System.out.println("3");
          for (int i = 0; i <iSize; i++){
  System.out.println("i=" + i + ", setting s to ");
             s = asData[index];
          }
      }
      asData[index] = s;
      iSize++;
  }

I took your code and changed a bit. Now it just gives back

  asData.length=2, iSize=2

1 2

Exception in thread "main" java.lang.IllegalArgumentException: index (0) must be greaterthan zero and less than size (2)
at MitchList.add(MitchList.java:42)
at MitchList.test(MitchList.java:14)
at MitchList.main(MitchList.java:10)

Any ideas?

Answer

It is very hard to get out what you are doing, with as little information you are giving us here, but this is what I’ve figured so far. Below is your code with some debugging statements in it.

I strongly recommend you add some information to your no-message exception. That is, change

throw new IllegalArgumentException();

to

throw new IllegalArgumentException("index (" + index + ") must be greater than zero and less than size (" + iSize + ")");

When I run this code, this is the output

[R:jeffyprogrammingsandboxxbnjava]java MitchList
asData.length=2, iSize=0
asData[0]=null
3
hello,
asData.length=2, iSize=1
2
Exception in thread "main" java.lang.IllegalArgumentException: index (1) must be greater than zero and less than
 size (1)
        at MitchList.add(MitchList.java:41)
        at MitchList.test(MitchList.java:14)
    at MitchList.main(MitchList.java:11)

You were hiding some very valuable information: Greater than zero and less than one is impossible

Also

  • You’re doubling the size of the array with data.length*2, yet you’re only increasing size by one: size++;.
  • You’re overwriting the value of s with every value in the array (the s = data[index]; in the for-loop), causing it to always equal the last element. Then you’re just re-inserting the current value of the last element over-top itself. In other words, the s parameter is being ignored, and the array is never changing.
  • One more thought: I think if(data[index] == "") should be if(data[index] == null). String arrays are initialized to null, not the empty-string.

Consider using the below code, with all its debugging statements, as a starting point for fixing the above issues, and discovering whatever others there may be. Good luck!

   import org.apache.commons.lang.StringUtils;
   /**
      <P>{@code java MitchList}</P>
    **/
   public class MitchList  {
      private String[] asData;
      private int iSize;
      public static final void main(String[] igno_red)  {
         MitchList ml = new MitchList();
         test(ml, 0, "hello");
         test(ml, 1, "goodbye");
      }
      public static final void test(MitchList m_l, int i_ndex, String s_tring)  {
         m_l.add(i_ndex, s_tring);

         //Print the array at the last index in asData
         System.out.println(StringUtils.join(m_l.asData, ", "));

        //If you don't have `commons.lang` installed, use this:
        //
        //   for(String s : asData)  {
        //      System.out.print(s + ", ");
        //   }
      }
      public MitchList() {
          asData = new String[2];
          iSize = 0;
      }

      public int iSize() {
          return iSize;
      }


          public void add(int index, String s){
System.out.println("asData.length=" + asData.length + ", iSize=" + iSize + "");
          if (asData.length == iSize) {
System.out.println("1");
              String[] newArray = new String[asData.length*2];
              for (int i=0; i < asData.length; i++) {
                  newArray[i] = asData[i];
              }
              asData = newArray;
      }
          if (index > 0 || index <iSize){
System.out.println("2");
             throw new IllegalArgumentException("index (" + index + ") must be greater than zero and less than size (" + iSize + ")");
          }
System.out.println("asData[" + index + "]=" + asData[index] + "");

          if (asData[index] != ""){
System.out.println("3");
              for (int i = 0; i <iSize; i++){
System.out.println("i=" + i + ", setting s to ");
                 s = asData[index];
              }
          }
          asData[index] = s;
          iSize++;
      }
}