I'm trying to replace values in an array while removing extra values. This is what I have, but it doesn't seem to work and I have no idea why.

I'm just challenging myself to not use any Strings in my project. This will be the last step.

/**
	 * 
	 * @param original The original array (will be overwritten)
	 * @param start The start index (inclusive) to replace
	 * @param end The end index (exclusive) to replace
	 * @param replacement The array to put starting at <bold>start</bold> and ending at 
	 * <code>replacement.length + start</code> **Note** If <code>end - start > replacement.length</code> 
	 * then all values at <code>replacement.length + start</code> to <code>end</code> will be removed.
	 */
	private static char[]replace(char [] original, int start, int end, char [] replacement){
		char[] lower = new char[start];
		for(int i = 0; i < start; i ++){
			lower[i] = original[i];
		}
		char[] upper = new char[original.length - end];
		for(int i = 0; i < original.length - end; i ++){
			upper[i] = original[i + end];
		}
		char[] full = new char[start + (original.length - end) + replacement.length];
		for(int i = 0; i < lower.length; i ++){
			full[i] = lower[i];
		}
		for(int i = 0; i < replacement.length; i ++){
			full[i + start] = replacement[i];
		}
		for(int i = 0; i < upper.length - end; i ++){
			full[i + end] = upper[i];
		}
		return full;
	}

it doesn't seem to work

it would save time if you described what it is doing now (show examples)
And describe what is wrong with it
And show what you want the results to look like

One way to show the contents of an array is to use the Arrays.toString() method.

The way you're going about this is a little odd. Why are you creating sub-arrays and copying over to them, and then copying them into your final array? Why not just fill in from 0..start from the original array, then fill from start+1..replacement.length from replacement, then fill again from original? You'll notice that this saves you one parameter, which may be the source of your trouble (what happens if end is passed in incorrectly?) and it also avoids screwing up the original array (which I see you went to some trouble about, good job there).

You'll want to do a bit of parameter validation - if start+replacement.length > original.length, you'll have to make sure you either back off (if you want the array to be returned with the same length) or adjust the return array's length (if you want to extend the original array to fit the new data).

Well, I solved it by using just System.arraycopy(), and I assume it's faster since it's performed by the VM (if not, please tell me so I can optimize it).

/**
	 * 
	 * @param original The original array (will be overwritten)
	 * @param start The start index (inclusive) to replace
	 * @param end The end index (exclusive) to replace
	 * @param replacement The array to put starting at <bold>start</bold> and ending at 
	 * <code>replacement.length + start</code> **Note** If <code>end - start > replacement.length</code> 
	 * then all values at <code>replacement.length + start</code> to <code>end</code> will be removed.
	 */
	private static char[]replace(char [] original, int start, int end, char [] replacement){
		end--;
		char[] temp = new char[original.length - (end - start) + replacement.length - 1];
		System.arraycopy(original, 0, temp, 0, start);
		System.arraycopy(replacement, 0, temp, start, replacement.length);
		if(end + 1 != original.length)
			System.arraycopy(original, end, temp, end, temp.length - end);
		return temp;
	}

Well, I solved it by using just System.arraycopy(), and I assume it's faster since it's performed by the VM (if not, please tell me so I can optimize it

arrayCopy is a native method, so I think it's safe to assume its faster than anything anyone can do in Java source code.

Yeah, that works, too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.