How do I add an element to the end of an array?
Code Java:
public class MP3_Player {
private String name;
private String maker;
private double total_storage;
private double used_storage;
private int num_of_mp3s;
public MP3[]mp3s = new MP3[num_of_mp3s];
private double battery;
private boolean on_switch;
public String getName(){
return name;
}
public void setName(String x){
name=x;
}
public String getMaker(){
return maker;
}
public void setMaker(String x){
maker=x;
}
public void add_mp3(MP3 x){
}
}
I'm trying to add an Object to the end of the array in the add_mp3 method.
Re: How do I add an element to the end of an array?
Quote:
add an Object to the end of the array
Code :
theArray[theIndexToEnd] = anObject; // add an object to the end
You need the index of where you want to add the object and then use it with the array.
Re: How do I add an element to the end of an array?
Re: How do I add an element to the end of an array?
That would put the object at the last element in the array.
Try it and see what happens.
Re: How do I add an element to the end of an array?
Quote:
Originally Posted by
Norm
Code :
theArray[theIndexToEnd] = anObject; // add an object to the end
Wouldn't that method set the object at that index to "anObject", that wouldn't add an element, only change it, right?
Re: How do I add an element to the end of an array?
Quote:
that wouldn't add an element, only change it, right?
Depends what you mean by add. What if the element at that index was null?
Arrays always have some value in all the slots. Initially they are null for arrays of objects.
Re: How do I add an element to the end of an array?
Quote:
Originally Posted by
Norm
Depends what you mean by add.
Well, by add, I took it as you have array arr1[] with size 5 and you want to add a sixth element. If that's the case, then there's a method in System to do it for you.
System (Java 2 Platform SE v1.4.2)
Re: How do I add an element to the end of an array?
What method is that? I see a method that will copy array elements but none that will change the size of an existing array.
Re: How do I add an element to the end of an array?
It would be the copy array method. It can easily be used to add elements to an array, or at least two arrays. Observe...
Code java:
public static int[] addArray(int[] arr1, int[] arr2)
{
int[] arrFin = new int[arr1.length+arr2.length];
System.arraycopy(arr1, 0, arrFin, 0, arr1.length);
System.arraycopy(arr2, 0, arrFin, arr1.length,arr2.length);
return arrFin;
}
It would be very easy to add one element by making an array of one element or make int[] arr2 into just an int.
Re: How do I add an element to the end of an array?
It looks like arraycopy() saves you writing a loop. I don't see where it adds any elements to an existing array.
Re: How do I add an element to the end of an array?
Code java:
public static int[] addArray(int[] arr1, int[] arr2) //1.
{
int[] arrFin = new int[arr1.length+arr2.length]; //2.
System.arraycopy(arr1, 0, arrFin, 0, arr1.length); //3.
System.arraycopy(arr2, 0, arrFin, arr1.length,arr2.length); //4.
return arrFin;
}
1. sets the parameters: two arrays to be added together
2. creates a new array to contain both of the parameter arrays
3. copies arr1 to arrFin
4. copies arr2 to arrFin
then return arrFin
Here is a crude example of how it could work:
Code java:
public static main(String[] args)
{
int[] grades = {90,80,85,60,70,100};
int[] semester2 = {80,90,90,95,100,70};
grades = addArray(grades, semester2);
System.out.println(Arrays.toString(grades));
}
Something like that. You are still adding the arrays together, and it is changing the size of grades. I haven't tried a more basic way, without using a method from System, it shouldn't be that hard though. Something like this maybe:
Code java:
public static int[] addArray(int[] arr1, int[] arr2)
{
int[] arrFin = new int[arr1.length+arr2.length];
int lastIndex = 0;
for(int i = 0; i < arr1.length; i++, lastIndex++)
{
arrFin[lastIndex] = arr1[i];
}
for(int i = 0; i < arr2.length; i++, lastIndex++)
{
arrFin[lastIndex] = arr2[i];
}
return arrFin;
}
However, am I correct in saying there is no way to make an existing array larger without something like this?
Re: How do I add an element to the end of an array?
It would be Simpler to say - there is no way to make an existing array larger.
You can copy its contents to another larger array.
Re: How do I add an element to the end of an array?
Ok, so to add an element to the end of an array, you have to create a larger array and do some copying, so something like this:
Code java:
public static int[] addToArray(int[] arr, int ele, int ind)//parameters are array to add to, an element you want to add, where you want to add it
{
int[] arrFin = new int[arr.length+1];//creates a new array with length arr.length+1
int lastIndex = 0;//indexing variable for arrFin
for(int i = 0; i < arrFin.length; i++, lastIndex++)//for loop to fill array will values from arr
{
if(lastIndex==ind)//adds ele to arrFin when lastIndex equals ind
{
arrFin[lastIndex] = ele;
lastIndex++;//increments lastIndex
}
arrFin[lastIndex] = arr[i];//adds the next element in arr
}
return arrFin;//returns the new array
}
Which is MFThomas's original question, right?