Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: I don't get the concept of how to insert an item into an array; exercise 170

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I don't get the concept of how to insert an item into an array; exercise 170

    Hi, so here's the problem. I don't really get the concept of how I "insert" an item into an array. I get a cannot find symbol error when I try to. I think its because I'm losing focus. Could someone clarify what sort of code would "insert" an item into an array please? I just want a goal of conceptually how I would do it.

    Anyways, here are the instructions for the exercise:

    Write a new class method, insert, for the Item class that takes three arguments — an Item[] array, an Item newItem, and an int k — and inserts newItem into array at index k, discarding the last item of the array (that is, the item originally at index array.length - 1).

    Here is the uneditable code:

     
    public class Item
    {
      private int myN;
     
      public Item( int n )
      {
        myN = n;
      }
     
      public String toString()
      {
        return "Item:" + myN;
      }
     
      public int getN() 
      {
        return myN;
      }
     
      public static Item[] makeItemArray( int len )
      {
        Item[] a = new Item[ len ];
        int i;
        for ( i = 0 ; i < len ; i++ )
          a[ i ] = new Item( i );
        return a;
      }
     
      public static void displayArray( Item[] array )
      {
        for ( Item item : array )
          System.out.println( item );
      }

    This part only I can edit:

     
    public static void insert( Item[] array, Item newItem, int k )
     
    {
     
      array[i - 1] = array[i];
     
    }

    But this part onward, I can't.

     
    }
     
    public class MainClass
    {
      public static void main( String[] args )
      {
        Item[] array = Item.makeItemArray(  );
     
        System.out.println( "Before: " );
        Item.displayArray( array );
     
        // make a new Item
        Item newItem = new Item( 99 );
     
        // insert the new item
        Item.insert( array, newItem,  );
     
        System.out.println( "\nAfter: " );
        Item.displayArray( array );
      }
    }

    I get a cannot find symbol error, but I thought I was doing as I was supposed to. I thought you had to have an ArrayList to be able to insert or delete an item from an array. How can you take a primitive object, like an array, and insert something into it. My idea of it was a[i] would be replaced with a[i + 1]. I know I'm getting this concept wrong because that's what I tried to do.

    Anyways, I've been really tired today and need a little bit of help because I'm having trouble keeping my thoughts straight today. I really appreciate the help.

    Sincerely, your pupil, ghostheadx



    PS: I am about to start Lab 28 if I get help today, which would make me caught up with my class, so today it's extremely important that I do this exercise correctly.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    I get a cannot find symbol error
    Please copy the full text of the error message and paste it here. It has important info about the error.

    I'm not sure what you mean by "insert"

    Given an array with 3 elements: ABC
    would doing an insert of D after A give:
    ADBC
    or
    ADC
    or
    ADB
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    @Norm

    Lol, I always forget to paste the whle error message. It says:

    "error: cannot find symbol

    array[i-a] = array[i]"

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    I agree with Norm that we need the error message, but I see that you're repeating old errors that we've covered before. for example, this line in main():

    Item[] array = Item.makeItemArray( );

    There is no Item method makeItemArray() that takes no arguments. Read the method's signature and call it correctly, supplying the necessary parameters.

    And that's not the entire error message, at least not if it's a compiler error. They don't look like that.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    The part about "Item[] array = Item.makeItemArray();" isn't editable. It's a part of the textbook. Why would they do that?

    --- Update ---

    I'm guessing they want me to define it afterwards? That's the only thing I can think of, because its defined before my code box so that I can't write it previous code. Is that still do-able?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    Why would they do that?
    It's called a typo. The code in the book was mistyped.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    What book are you using? Give the book, section, problem number, and page this comes from.

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    I'm using the eimacs.com online textbook. It's exercise 170. The section is Insertions, subsection #3. It's on page 265.

    --- Update ---

    By the way, I posted in this old thread you told me to research about my the AP concepts:

    http://www.javaprogrammingforums.com...concepts.html?

    I know it's unrelated to the current thread, but I thought it would be a good idea to post it in here.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    Can't get my eyes on the book. I'll agree with Norm that it's a misprint for now, but most times that I've been able to see what the poster is reading have resulted in discovering that there is a misunderstanding.

    You've shown us two "uneditable" sections that conflict. Either the book is wrong (the book is in the 5th+ edition, so those errors should be fixed), or there's some misunderstanding between you and the book. Reread the question and directions carefully.

  10. #10
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    I copied the directions from the book to the text book with copy paste:

    Write a new class method, insert, for the Item class that takes three arguments — an Item[] array, an Item newItem, and an int k — and inserts newItem into array at index k, discarding the last item of the array (that is, the item originally at index array.length - 1).

    So my interpretation was that I have to write a class, called insert, that takes three parameters and inserts every new item into an array at a certain index, discarding the last item right?

    I think the textbook has a typo, so I'll just skip it. Thanks anyways.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    Yes, insert newItem into the Item[] array at index k, dropping the existing last item from the array, so moving all existing elements past index k up by one. Your interpretation of the part you quoted seems okay, but the confusion was caused by your understanding of what code in the book was editable, what wasn't editable, and whether the uneditable parts were correct.

  12. #12
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't get the concept of how to insert an item into an array; exercise 170

    @GregBrannon

    I agree with you. I still don't think I get that part of the book though.

    --- Update ---

    I mean, that exercise per-say.

Similar Threads

  1. How to get a random item from an array?
    By zhider in forum Java Theory & Questions
    Replies: 2
    Last Post: December 15th, 2012, 09:13 PM
  2. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  3. method to return the position of the smallest item in an array
    By izzahmed in forum Java Theory & Questions
    Replies: 5
    Last Post: November 4th, 2011, 04:18 AM
  4. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM
  5. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM

Tags for this Thread