Jump to content




Java arrays

java

5 replies to this topic

#1 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 20 April 2013 - 08:04 AM

This intended for the experienced Java programmer, I want to show my Java code that manipulates arrays and I gladly want to improve it. However, one thing: I know its better to use arraylists, but this is just some practise for my Java programming skills.

Well here is my "array-util" code:
public String[] removeFrom(String[] array, int index)
	{
		String[] newArray = new String[array.length - 1];
		int newIndex = 0;
		for(int i = 0; i < array.length; i++)
		{
			if(i != index)
			{
				newArray[newIndex] = array[i];
				newIndex++;
			}
		}
		
		array = null;
		return newArray;
	}
	
	//Set index to -1 to add it at the end
	public String[] addItem(String[] array, String item, int index)
	{
		String[] newArray = new String[array.length + 1];
		int realIndex = 0;
		for(int i = 0; i < array.length; i++)
		{
			if(i == index)
			{
				newArray[i] = null;
			} else {
				newArray[i] = array[realIndex];
				realIndex++;
			}
		}
		if(index == -1)
		{
			newArray[array.length + 1] = item;
		} else {
			newArray[index] = item;
		}
		
		array = null;
		return newArray;
	}
	
	public String[] replaceIndex(String[] array, int index, String newString)
	{
		String[] newArray = new String[array.length];
		for(int i = 0; i < array.length; i++)
		{
			if(i == index)
			{
				newArray[i] = newString;
			} else {
				newArray[i] = array[i];
			}
		}
		array = null;
		return newArray;
	}

You could say I have no experience in Java programming, so thats why I made this.

I would love some feedback on how to improve things, or even feedback to let me know this is good :P

Thank you in advance,

- Engineer

#2 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 20 April 2013 - 10:14 AM

Nice. But you could use templates(or whatever they're called in Java), so that it doesn't care about type. And you could create something like a class Container<T>, which would then do all of the stuff to member variables.(hard to explain, I'm tired as shit)

#3 ElvishJerricco

  • Members
  • 803 posts

Posted 20 April 2013 - 10:51 AM

A lot of you code could be vastly simplified by using System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 20 April 2013 - 10:56 AM

View PostMads, on 20 April 2013 - 10:14 AM, said:

Nice. But you could use templates(or whatever they're called in Java), so that it doesn't care about type. And you could create something like a class Container<T>, which would then do all of the stuff to member variables.(hard to explain, I'm tired as shit)
Then Im going to ask you: Can you explain it when you are less tired?:P

View PostElvishJerricco, on 20 April 2013 - 10:51 AM, said:

A lot of you code could be vastly simplified by using System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
I was not aware of this, but I think it will make the cody kind of useless. Thanks anyway!:D

#5 ElvishJerricco

  • Members
  • 803 posts

Posted 21 April 2013 - 06:55 PM

EDIT: Sorry i wanted to post a lot but the formatting was tripping out because HTML and <> in my [CODE] tags didn't get along

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 April 2013 - 07:00 PM

View PostMads, on 20 April 2013 - 10:14 AM, said:

But you could use templates(or whatever they're called in Java), so that it doesn't care about type.
Generics... Thought they were called that in all languages, hmmm...

EDIT: Just read the one above, lol...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users