C++ code example using a vector object (dynamic array):
#include<iostream>
#include<vector>
using namespace std;
typedef std::vector<int> vec;
typedef std::vector<int>::iterator it;
void init_array(vec& v)
{
v.clear();
for(int i=1; i<=10; ++i)
v.push_back(i);
}
void print_array(vec& v)
{
for(it i=v.begin(); i!=v.end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
it get_element(vec& v, unsigned int index)
{
it i=v.begin();
while(index--)
++i;
return(i);
}
int main()
{
vec v;
init_array(v);
cout<<"Initial array:\t";
print_array(v);
// Remove element at index 3 (the 4th element).
v.erase( get_element(v, 3));
cout<<"Modified array:\t";
print_array(v);
return(0);
}
Since Java is object-oriented, you can use a similar approach to the above. However, with C, there's a bit more work involved in managing the memory allocation, however the overall approach is the same:
#include<iostream>
using namespace std;
int* init_array(unsigned int len)
{
int* p=(int*)malloc(len*sizeof(int));
for(unsigned int i=0; i<len; ++i)
p[i]=i+1;
return(p);
}
void print_array(int* p, unsigned int len)
{
for(unsigned int i=0; i<len; ++i)
cout<<p[i]<<" ";
cout<<endl;
}
int main()
{
unsigned int size;
int* p;
size=10;
p=init_array(size);
cout<<"Initial array:\t";
print_array(p,size);
// Remove element at index 3 (the 4th element)
--size;
for( unsigned int y=3; y<size; ++y)
p[y]=p[y+1];
p=(int*)realloc(p,size*sizeof(int));
cout<<"Modified array:\t";
print_array(p,size);
delete [] p;
return(0);
}
You cannot delete from an array.
// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;
Set it to null
You cannot delete elements from an array. But you can move the elements: if (del_index < no_of_elements-1) { memmove (&array [del_index], &array [del_index+1], sizeof (array [0]) * (no_of_elements - del_index - 1)); } --no_of_elements;
First locate the position of an array by search after than use a delete function to delete an array
Select the entire array and press the Delete key. There are usually only problems if you want to delete some, but not all of the cells in the array.
To detect the duplicate, you will have to write a nested loop that compares each element with all the previous elements.To actually delete the duplicate, once you find it, you have to move over all the elements after the duplicate. If the order of the elements doesn't matter, it is faster to just move the LAST array element, overwriting the duplicate element. Use a variable to keep track how many elements of the array are "usable". For example, if your array had 10 elements, and you delete 1, the array size will still be 10... but (after moving the elements over) only 9 of those elements have useful information.
If the array is dynamic then use a vector instead of an array. You can then use the vector::erase() function to delete an element or a range of elements. Remember that if the vector contains pointers to unshared memory, then you must release the pointer before erasing the element containing that pointer. If the array is static then you cannot delete elements. The assumption with static arrays is that you will neither add nor delete, you will only modify existing elements. However, you can emulate a deletion by shunting elements to the left, and keeping track of how many used elements there are (which must always be less than or equal to the upper bound plus one). Again, if the array contains pointers to unshared memory, you must release the pointer before shunting elements. You can also do the same thing with dynamic C-style arrays, but once you've shunted elements to the left you can reallocate the array with the new size to physically delete the final element.
If the array was allocated with new, then delete it with delete []. Otherwise, if it was allocated with malloc() then delete it with free. Otherwise, you cannot delete it because it was pre-allocated at link-load time by the compiler.
C compiler delete only those memory which is dynamically created. So we can't delete the value of array.
The value of the kth smallest element in the array is the kth element when the array is sorted in ascending order.
One advantage of a linked list (with pointers) is that it is fairly cheap to insert or delete an element - once you know where it is. A disadvantage is that getting a specific element - for example, the 1000th. element - is expensive.