14 Replies - 5039 Views - Last Post: 02 February 2013 - 03:16 PM Rate Topic: -----

#1 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:19 PM

I am working on a java assignment where I need to delete an integer element in an array and shift the below elements up on space to keep them in order. The array is currently random integers in descending order. I am not allowed to use array.copy because I will need to collect array usage information as part of the assignment. I have tried a ton of different ways of doing this but cannot seem to get it working. Thank you!

public static void deletionArray(int anArray[], int positionToDelete) {
		for (int j = anArray[positionToDelete] - 1; j < anArray.length; j++) {
			System.out.println("j is " + j);
			anArray[j] = anArray[j + 1];
		}
		
		displayArray(anArray);
	}	


Is This A Good Question/Topic? 0
  • +

Replies To: How do I shift array elements up one position in java?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:27 PM

Is the array completely populated? What takes the place of the last element advanced forward?

What you have posted should work except that you can't iterate to the end of the array and then use the index endOfTheArray + 1. Try iterating to one before the end of the array.
Was This Post Helpful? 2
  • +
  • -

#3 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:31 PM

Sorry, I should have been more clear. The array is 10 elements long but I only have 5 filled. I tried;

for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
			System.out.println("j is " + j);
			anArray[j] = anArray[j + 1];
		}
		
		displayArray(anArray);
	}


But I get the same result. The array doesn't seem to delete the position by moving all of the other elements up.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7772
  • View blog
  • Posts: 13,139
  • Joined: 19-March 11

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:32 PM

There is a useful method in the System class, called System.arraycopy. It is, however, a little bit intimidating. I suggest you review the documentation, and if it seems helpful, try to use it.
Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:34 PM

And what are those same results? Show the array before and after you've applied your solution.
Was This Post Helpful? 1
  • +
  • -

#6 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:34 PM

View Postjon.kiparsky, on 02 February 2013 - 02:32 PM, said:

There is a useful method in the System class, called System.arraycopy. It is, however, a little bit intimidating. I suggest you review the documentation, and if it seems helpful, try to use it.


I am not allowed to use array.copy as I need to collect array usage information. Sadly, I have to do it manually.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7772
  • View blog
  • Posts: 13,139
  • Joined: 19-March 11

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:37 PM

View Postjoerdie, on 02 February 2013 - 04:31 PM, said:

Sorry, I should have been more clear. The array is 10 elements long but I only have 5 filled. I tried;

for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
			System.out.println("j is " + j);
			anArray[j] = anArray[j + 1];
		}
		
		displayArray(anArray);
	}


But I get the same result. The array doesn't seem to delete the position by moving all of the other elements up.



If you only have five filled, then you probably only want to copy five, no?

Let's try clarifying a little. What is the initial state of your array, and what would you like it to look like when you're done?
Was This Post Helpful? 1
  • +
  • -

#8 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:38 PM

View PostGregBrannon, on 02 February 2013 - 02:34 PM, said:

And what are those same results? Show the array before and after you've applied your solution.


public static void deletionArray(int anArray[], int positionToDelete) {
		displayArray(anArray);
		for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
			System.out.println("j is " + j);
			anArray[j] = anArray[j + 1];
		}
		
		displayArray(anArray);
	}		


Gives me,
97
86
61
46
3
0
0
0
0
0
97
86
61
46
3
0
0
0
0
0
Was This Post Helpful? 0
  • +
  • -

#9 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:43 PM

Quote

If you only have five filled, then you probably only want to copy five, no?

Let's try clarifying a little. What is the initial state of your array, and what would you like it to look like when you're done?


Yes, I could move just the five. The initial state would be 5 integers followed by zeros since there is nothing there. When I am done, I am looking to have one of those integers deleted and the integers below the deleted one moved up one position. I suppose I would then have an extra 0 at the bottom of my array.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7772
  • View blog
  • Posts: 13,139
  • Joined: 19-March 11

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:47 PM

So you have {97,86,61,46,3,0,0,0,0,0} and you want {0, 97,86,61,46,3,0,0,0,0}?

public static void deletionArray(int anArray[], int positionToDelete) {
		displayArray(anArray);
		for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
			System.out.println("j is " + j);
			anArray[j] = anArray[j + 1];
		}
		
		displayArray(anArray);
	}		




Oh, hang on. What's this? int j = anArray[positionToDelete] - 1;

What is that value?

EDIT: If you don't see why this is a problem, print out that value. Then fix your program and enjoy the rest of your day.

This post has been edited by jon.kiparsky: 02 February 2013 - 02:49 PM

Was This Post Helpful? 2
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:54 PM

Good catch, Jon. I didn't see that until I tried to run it.
Was This Post Helpful? 1
  • +
  • -

#12 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 02:57 PM

View Postjon.kiparsky, on 02 February 2013 - 02:47 PM, said:

So you have {97,86,61,46,3,0,0,0,0,0} and you want {0, 97,86,61,46,3,0,0,0,0}?

public static void deletionArray(int anArray[], int positionToDelete) {
		displayArray(anArray);
		for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
			System.out.println("j is " + j);
			anArray[j] = anArray[j + 1];
		}
		
		displayArray(anArray);
	}		




Oh, hang on. What's this? int j = anArray[positionToDelete] - 1;

What is that value?

EDIT: If you don't see why this is a problem, print out that value. Then fix your program and enjoy the rest of your day.


I am looking to go from this {95,90,81,41,7,0,0,0,0,0} getting rid of say the 90 to this. {95,81,41,7,0,0,0,0,0,0}

anArray[positionToDelete] is the position I am trying to delete. I can, and have printed that value while trying to solve this issue. the - 1 bit is there because I am advancing my loop before I change anything. So if I were using the above arrays as an example, anArray[positionToDelete] would be 90 in position 1. The -1 would make the position 0 (95) so that when the I go into the block I am back to position 1.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7772
  • View blog
  • Posts: 13,139
  • Joined: 19-March 11

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 03:06 PM

View Postjoerdie, on 02 February 2013 - 04:57 PM, said:

anArray[positionToDelete] is the position I am trying to delete. I can, and have printed that value while trying to solve this issue.



Not exactly. anArray[positionToDelete] is the value at that position. positionToDelete is the index: that's the "position" you're trying to work with. If positionToDelete is 1 in the current example (and let's please just stick with one sample array, they're each as good as the next) then what is the value of anArray[positionToDelete]?

And what happens when you plug that value into your loop
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
  anArray[j] = anArray[j + 1];
}


(The current array is the one you mentioned most recently: {95,90,81,41,7,0,0,0,0,0} )
Was This Post Helpful? 1
  • +
  • -

#14 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 03:15 PM

View Postjon.kiparsky, on 02 February 2013 - 03:06 PM, said:

View Postjoerdie, on 02 February 2013 - 04:57 PM, said:

anArray[positionToDelete] is the position I am trying to delete. I can, and have printed that value while trying to solve this issue.



Not exactly. anArray[positionToDelete] is the value at that position. positionToDelete is the index: that's the "position" you're trying to work with. If positionToDelete is 1 in the current example (and let's please just stick with one sample array, they're each as good as the next) then what is the value of anArray[positionToDelete]?

And what happens when you plug that value into your loop
for (int j = anArray[positionToDelete] - 1; j < anArray.length - 1; j++) {
  anArray[j] = anArray[j + 1];
}


(The current array is the one you mentioned most recently: {95,90,81,41,7,0,0,0,0,0} )


That did it thank you. I can't believe I have been on this for 4 hours. Thanks.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7772
  • View blog
  • Posts: 13,139
  • Joined: 19-March 11

Re: How do I shift array elements up one position in java?

Posted 02 February 2013 - 03:16 PM

Glad I could help. Now go run around the block or something, you'll be needing it.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1