Closed Thread
Page 3 of 3 FirstFirst 123
Results 21 to 26 of 26

Thread: How to replace an existing data in an array

  1. #21
    Xdawn90 is offline Learning Programmer
    Join Date
    Jul 2009
    Location
    in a fantasy world ...
    Posts
    54
    Rep Power
    0

    Re: How to replace an existing data in an array

    hmm ... I see. Thanks for the info.

    I have another question here : How am I going to delete a record(containing data) in an array ?

    Thanks very much.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #22
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: How to replace an existing data in an array

    Code:
    ardObject[0] = null;
    Maybe?

  4. #23
    Xdawn90 is offline Learning Programmer
    Join Date
    Jul 2009
    Location
    in a fantasy world ...
    Posts
    54
    Rep Power
    0

    Re: How to replace an existing data in an array

    If I were to use that solution, will I get an error if I iterate through the array ?

    Can you suggest some suitable solution for me to delete a particular object , containing fields of data, inside an array ?

    Thanks.

  5. #24
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: How to replace an existing data in an array

    Well if you try to access that item then yes.

    Code:
    for (int i=0;i<length;i++) {
         if (arr[i] == null) {
              continue; // no item here so ignore this entry
         }
         // process item here
    }

  6. #25
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: How to replace an existing data in an array

    Wrote useful little function, that removes element from array and fills the hole with other elements... so if [1,2,3,4,5] and "3" gets removed, array will be [1,2,4,5,null]

    Code:
      public static void removeIndex(Object array[], int index){
        	if(index >= array.length || index < 0) return; //wrong index
        	int b = 0;
        	for(int i = 0; i < array.length; i++){
        		if(i == index) b = 1;
        		else{
        			array[i-b] = array[i];
        		}    		
        	}    	
        	array[array.length-1] = null;
       }
    here's another one for inserting. it will push elements forward and if array is too small, then last one will be lost.

    Code:
    public static void insertIndex(Object ob, Object array[], int index){
        	if(array.getClass().getComponentType() != ob.getClass()) return; //object and array doesn't match.
        	if(index >= array.length || index < 0) return; //wrong index    	    	
        	for(int i = array.length-1; i >= 0; i--){
        		if(i > index)
        			array[i] = array[i-1];
        		else if(i == index){ 
        			array[i] = ob;
        			break;
        		}
        	}        	
    }
    Last edited by Sinipull; 07-07-2009 at 08:42 AM.

  7. #26
    Xdawn90 is offline Learning Programmer
    Join Date
    Jul 2009
    Location
    in a fantasy world ...
    Posts
    54
    Rep Power
    0

    Re: How to replace an existing data in an array

    Thanks very much guys. I really appreciate it.

Closed Thread
Page 3 of 3 FirstFirst 123

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. use of array to read student data
    By jackson6612 in forum C and C++
    Replies: 15
    Last Post: 06-08-2011, 07:57 AM
  2. php!? How to get data from DB into ASSOC ARRAY?
    By Stasonix in forum PHP Development
    Replies: 1
    Last Post: 03-24-2011, 07:09 AM
  3. Replies: 3
    Last Post: 08-21-2010, 02:59 AM
  4. C++ Reading Data file into Struct Array HELP!!!
    By kevinsabres in forum C and C++
    Replies: 6
    Last Post: 04-21-2010, 11:48 AM
  5. Can you use vblookup to replace data in Excel VB
    By stackemevs in forum Visual Basic Programming
    Replies: 2
    Last Post: 01-17-2010, 06:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

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