add an element to a string array

i am writing a method to add a string element to a place in the array

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 66716 Views - Last Post: 11 March 2009 - 04:18 PM Rate Topic: -----

#1 amooseinabox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-March 09

add an element to a string array

Posted 10 March 2009 - 07:58 PM

The method i am writing is to add a element to the array in a specific location. the method is given (String[] arr, int pos, String name).
here is the code i have written so far:
static public void addStringElement(String[] arr, int pos, String name){
		String[] copy = new String[arr.length+1];
		for (int index = 0; index < arr.length+1; index++){
			if (index == pos){
				copy[index] = name;
			}else{
				copy[index] = arr[index-1];
			}
		}
		arr = copy;
	}


all its doing is returning the exact array that i had in the first place. without the new element added.

Is This A Good Question/Topic? 0
  • +

Replies To: add an element to a string array

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: add an element to a string array

Posted 10 March 2009 - 08:07 PM

your method header is messed up
public static String[] addStringElement(String[] arr, int pos, String name){

		arr[pos] = name;
		
		return arr;
	}


This post has been edited by mostyfriedman: 10 March 2009 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: add an element to a string array

Posted 10 March 2009 - 08:08 PM

you are not returning anything it is void.
You are assigning the parameter the copy and that is it, you want to make it return String[] then return copy.

Quote

for (int index = 0; index < arr.length+1; index++){
if (index == pos){
copy[index] = name;
}else{
copy[index] = arr[index-1];
}
}

You start index at 0 then do arr[index - 1], what if the first index is not the one being added at? you will not add at -1, you have to do an additional checkings
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: add an element to a string array

Posted 10 March 2009 - 08:08 PM

why would you need a method for that though
Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: add an element to a string array

Posted 10 March 2009 - 08:11 PM

Basically he is doing the behavior of an ArrayList, only with Strings only, and only increasing the size by by 1 each time.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: add an element to a string array

Posted 10 March 2009 - 08:26 PM

View Postmostyfriedman, on 10 Mar, 2009 - 07:08 PM, said:

why would you need a method for that though

Still learning array and not learning ArrayList yet
Good pedagogical approach as far as I am concerned. Never have problem to answer to that type of question/problem
Then when you move to ArrayList you have kind of an understandment on how they can work
Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: add an element to a string array

Posted 10 March 2009 - 08:38 PM

This is what we did in Data Structures last year, as we talked about the data structures we coded the "clone" to the Java API class to know how it worked.

This is what we did for ArrayList except it was generic and we had optimization in it for size, so wouldn't just increase by 1 each time, instead only sometimes by a 3/2 factor.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: add an element to a string array

Posted 10 March 2009 - 08:57 PM

View PostBigAnt, on 10 Mar, 2009 - 07:38 PM, said:

This is what we did in Data Structures last year, as we talked about the data structures we coded the "clone" to the Java API class to know how it worked.

This is what we did for ArrayList except it was generic and we had optimization in it for size, so wouldn't just increase by 1 each time, instead only sometimes by a 3/2 factor.

Boaf... you learned the principle... that's what counts
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: add an element to a string array

Posted 11 March 2009 - 03:28 AM

You should take a look at System.arraycopy. This can take advantage of native optimisations, so is potentially more efficient. Used by API for array manipulation
Was This Post Helpful? 1
  • +
  • -

#10 amooseinabox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-March 09

Re: add an element to a string array

Posted 11 March 2009 - 03:21 PM

o.k. thanks for the help ive modified my code to use the System.arraycopy i get it to add the element. but this time it deletes the last term of the original array. the code i am writing is not for re usability, just for the task of adding the element to the first position of the array.
 static public void addStringElement(String[] arr, int pos, String name){
		String[] copy = new String[arr.length+2];
		System.arraycopy(arr,0,copy,1,arr.length);
		copy[pos] = name;
		System.arraycopy(copy,0,arr,0,arr.length);
	}


This post has been edited by amooseinabox: 11 March 2009 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: add an element to a string array

Posted 11 March 2009 - 03:29 PM

View Postg00se, on 11 Mar, 2009 - 02:28 AM, said:

You should take a look at System.arraycopy. This can take advantage of native optimisations, so is potentially more efficient. Used by API for array manipulation

If you care about optimisation do not re-invent the wheel and use an ArrayList
Was This Post Helpful? 0
  • +
  • -

#12 amooseinabox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-March 09

Re: add an element to a string array

Posted 11 March 2009 - 03:32 PM

yeah i know that array lists are easier, but i am restricted to using arrays for this current problem. if arraylists were allowed i would have been done awhile ago.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: add an element to a string array

Posted 11 March 2009 - 03:38 PM

View Postamooseinabox, on 11 Mar, 2009 - 02:32 PM, said:

yeah i know that array lists are easier, but i am restricted to using arrays for this current problem. if arraylists were allowed i would have been done awhile ago.

The reply was for g00se.. we know that you have to use array
Why do you create your new array of length+2 should be +1 if you add just one element
Was This Post Helpful? 0
  • +
  • -

#14 amooseinabox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-March 09

Re: add an element to a string array

Posted 11 March 2009 - 03:42 PM

umm.. actually i have no clue why i did that. it might have just been a precaution to avoid the arrayoutofbounds error.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: add an element to a string array

Posted 11 March 2009 - 03:48 PM

View Postamooseinabox, on 11 Mar, 2009 - 02:42 PM, said:

umm.. actually i have no clue why i did that. it might have just been a precaution to avoid the arrayoutofbounds error.

but then if you loop on array.length you'll get a null pointer exception on the last element
better to go back and fix your first version... with minor fixes

[
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2