Results 1 to 8 of 8
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Adding and Removing elements from arrays

    Hi. I have a general question and a specific question. I was wondering: How I can add element(s) and remove element(s) from an array.

    I understand the algorithm.

    for adding element(s):
    1. create a new array with a new size + element added
    2. go through a for loop and add it in

    for removing element(s):
    1. create a new array with a new size - elements removed
    2. go through for loop and take it out




    I was wondering if someone could provide a code snippet or segment to illustrate how this works.

    For example given a method:
    Java Code:
    public void add (List a)
    
    //a = a list of items that are to be added into the current object
    thank you for your time.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: Adding and Removing elements from arrays

    You have the algorithm.
    I suggest you try to write it and then come back if/when you hit a problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Adding and Removing elements from arrays

    here's what i have:

    I want to add a type List a into an array:


    Java Code:
    add_arr = new Listable[add_arr.length + 1];
    
    add_arr[add_arr.length] = a;
    }

    I am confused most particularly on how to add one element into an array while keeping the rest.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: Adding and Removing elements from arrays

    You need to assign the existing data as well as the new one, so you need to 'remember' the original data somewhere.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Adding and Removing elements from arrays

    heres is a specific example I need it help with:

    algorithm:

    1. create new array
    2. copy items from original array to new array up to point where item is inserted (items have to be sorted in order...original is already sorted)
    3. make new array at some position = to added item
    4. copy items from original array to new array after that point


    Java Code:
    //note: original array is items[];
    
    public void add(Listable itemToAdd) {   //I want to add item called itemToAdd of type Listable
    
    		//if itemToAdd value < add value --- return negative
    		//if itemToAdd value == add value --- return 0
    		// if itemToAdd value > add value --- return positive
    
    
    		Listable[] add = new Listable [items.length + 1];  //new array of type Listable
    
    		int count = 0; //counter
    		int i = 0; //index counter
    		while (itemToAdd.getName().compareTo(add[i].getName()) > 0){    //if itemToAdd's string value is greater than 
                                                                                                                    //add[i]'s string value, it returns a positive
    
    			add[i] = items[i];      //copy all of original values into new array
    			i++;                        //increment
    			count++;                //increment
    
    		}
    
    		add[count] = itemToAdd;   //once while conditions fails, add[count] will equal the added itemToAdd
    		count++;   
    
    		for (int j = count; j < add.length; j++){	//from current count to end
    			add[j] = items[j-1];	                       // copy elements from original to add[]
    
    
    		}
    	}
    It's not working. Please help thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: Adding and Removing elements from arrays

    What does "it's not working" mean?
    What happens that shouldn't be happening?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Adding and Removing elements from arrays

    the tests says theres a null pointer

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: Adding and Removing elements from arrays

    Which means there should be an exception and a stack trace.
    The stack trace will tell you which line the null pointer is occurring on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. removing from a hashmap and adding to the end
    By dogeatdogfood in forum New To Java
    Replies: 2
    Last Post: 05-05-2012, 01:34 AM
  2. removing and adding SWING components
    By Athlon* in forum AWT / Swing
    Replies: 1
    Last Post: 12-06-2011, 06:12 PM
  3. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  4. Adding and removing panels dynamically
    By kbyrne in forum AWT / Swing
    Replies: 1
    Last Post: 04-12-2008, 08:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •