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:
Code Java:
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:
Code Java:
public static void insert( Item[] array, Item newItem, int k )
{
array[i - 1] = array[i];
}
But this part onward, I can't.
Code Java:
}
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
:confused:
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.
Re: I don't get the concept of how to insert an item into an array; exercise 170
Quote:
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
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]"
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.
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?
Re: I don't get the concept of how to insert an item into an array; exercise 170
Quote:
Why would they do that?
It's called a typo. The code in the book was mistyped.
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.
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. :)
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.
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.
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.
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.