Solved

Shifting Array Element Left

Posted on 2011-03-09
5
463 Views
Last Modified: 2012-05-11
Hello All

I am trying to shift an element to the left.  I want 5050 to become 0550.

Here's my code.  I don't know what I'm doing wrong.

class TestArrays {
	public static void main(String[] args) {
		int[] ia = new int[101];
		for (int i = 0; i < ia.length; i++)
			ia[i] = i;
		int sum = 0;
		for (int i = 0; i < ia.length; i++)
			sum += ia[i];
		System.out.println(sum);
		
		sum = 0;
		System.arraycopy(ia, 1, ia, 0, ia.length-1);
		for (int i = 0; i < ia.length; i++)
			sum += ia[i];

      System.out.println(sum);
		
	}
	}

Open in new window


Can anyone show me how to shift an element left?

Thanks

RichW
0
Comment
Question by:RichW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35089757
Sorry how would 5050 become 0550 by shifting?
do you mean 5050 ---> 0505 ?
0
 
LVL 4

Author Comment

by:RichW
ID: 35089769
If you shift the first 0 to the left, it would be 0550, no?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35089791
OK, so you have an array - your task is to move element at posiion j (j>0) to position j-1, and you don't care
what happens with the previous value of element j-1 - and what happens with the new element j either - correct?

Then why woould not you jsut simply say a[j-1] = a[j];

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35089808
Or you want to exchange elements at position j and j-1 - that would be another task, but also simple:

int temp = a[j-1];
a[j-1]=a[j];
a[j] = temp;

0
 
LVL 35

Expert Comment

by:mccarl
ID: 35089830
So what is your requirement for shifting then? To find the first zero and shift it one place to the left? Why the zero and why only the first zero?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 158
numbers ascending pyramid 101 262
HashTable highest marks enumeration alternative 9 59
Java exception bubble up 2 26
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question