This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Engineer's profile picture

Java arrays

Started by Engineer, 20 April 2013 - 07:04 AM
Engineer #1
Posted 20 April 2013 - 09: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
Mads #2
Posted 20 April 2013 - 11: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)
ElvishJerricco #3
Posted 20 April 2013 - 11: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)
Engineer #4
Posted 20 April 2013 - 11:56 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)
Then Im going to ask you: Can you explain it when you are less tired?:P/>

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/>
ElvishJerricco #5
Posted 21 April 2013 - 07:55 PM
EDIT: Sorry i wanted to post a lot but the formatting was tripping out because HTML and <> in my
 tags didn't get along
theoriginalbit #6
Posted 21 April 2013 - 08:00 PM
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…