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 13 of 13

Thread: How do I add an element to the end of an array?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow How do I add an element to the end of an array?

    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.


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

    Default Re: How do I add an element to the end of an array?

    add an Object to the end of the array
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I add an element to the end of an array?

    Will
    mp3s[mp3s.length-1] = x
    place x at the end?

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

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How do I add an element to the end of an array?

    Quote Originally Posted by Norm View Post
    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?

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

    Default Re: How do I add an element to the end of an array?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How do I add an element to the end of an array?

    Quote Originally Posted by Norm View Post
    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)

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

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default 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...

    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.

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

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How do I add an element to the end of an array?

    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:

    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:

    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?

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

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default 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:

     
    	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?

Similar Threads

  1. eliminating an element in array
    By hanazz69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2012, 05:33 AM
  2. add new element in array
    By allen_k in forum Collections and Generics
    Replies: 3
    Last Post: December 11th, 2011, 03:13 PM
  3. End Element exception reading XML file
    By treshr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 5th, 2011, 12:58 AM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  5. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 08:50 PM

Tags for this Thread