COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### array prob

• Email
• Follow

```hi everone,

i am trying to write a function called shiftDown.
This function needs to move the contents of an array one place back .
ex. int[] intarray = new int[4]

intarray[0] = 1;
intarray[1] = 2;
intarray[2] = 3;
intarray[3] = 4;

then i want to use the function and i must have the following: intarray[0]
must then be 2
intarray[1] must be 3
intarray[2] must be 4
and intarray[3] must be 1

i am working on this for days now and still can't figure it out    this is
what i allready have but it doesn't work

public void shiftDown() {
for (int i = 0; i < lengte2.length; i++) {
if ( lengte2[i] == lengte2.length) {
lengte2[i] = lengte2[0];
lengte2[i] = lengte2[i + 1];
}
}
}

lengte2 is my array.

TIA

Tommaz

```
 0
Reply tommaz (9) 9/28/2003 3:47:15 PM

See related articles to this posting

```Tom Wouters <Tommaz@pandora.be> scribbled the following:
> hi everone,

> i am trying to write a function called shiftDown.
> This function needs to move the contents of an array one place back .
> ex. int[] intarray = new int[4]

>         intarray[0] = 1;
>         intarray[1] = 2;
>         intarray[2] = 3;
>         intarray[3] = 4;

> then i want to use the function and i must have the following: intarray[0]
> must then be 2
>                intarray[1] must be 3
>                intarray[2] must be 4
>         and intarray[3] must be 1

> i am working on this for days now and still can't figure it out    this is
> what i allready have but it doesn't work

> public void shiftDown() {
>   for (int i = 0; i < lengte2.length; i++) {
>   if ( lengte2[i] == lengte2.length) {
>     lengte2[i] = lengte2[0];
>     lengte2[i] = lengte2[i + 1];
>   }
>   }
>  }

> lengte2 is my array.

Here's an algorithm for you. Put lengte2[0] into some temporary variable.
Then for all i in 1 to lengte[2].length-1, put lengte2[i] into
lengte2[i-1]. Finally put the temporary variable into
lengte2[lengte2.length-1].

Just write this algorithm out in Java and you've solved your homework.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
```
 0
Reply palaste (2323) 9/28/2003 3:54:31 PM

```"Joona I Palaste" <palaste@cc.helsinki.fi> schreef in bericht
news:bl707n\$quv\$1@oravannahka.helsinki.fi...
> Tom Wouters <Tommaz@pandora.be> scribbled the following:
> > hi everone,
>
> > i am trying to write a function called shiftDown.
> > This function needs to move the contents of an array one place back .
> > ex. int[] intarray = new int[4]
>
> >         intarray[0] = 1;
> >         intarray[1] = 2;
> >         intarray[2] = 3;
> >         intarray[3] = 4;
>
> > then i want to use the function and i must have the following:
intarray[0]
> > must then be 2
> >                intarray[1] must be 3
> >                intarray[2] must be 4
> >         and intarray[3] must be 1
>
> > i am working on this for days now and still can't figure it out    this
is
> > what i allready have but it doesn't work
>
> > public void shiftDown() {
> >   for (int i = 0; i < lengte2.length; i++) {
> >   if ( lengte2[i] == lengte2.length) {
> >     lengte2[i] = lengte2[0];
> >     lengte2[i] = lengte2[i + 1];
> >   }
> >   }
> >  }
>
> > lengte2 is my array.
>
> Here's an algorithm for you. Put lengte2[0] into some temporary variable.
> Then for all i in 1 to lengte[2].length-1, put lengte2[i] into
> lengte2[i-1]. Finally put the temporary variable into
> lengte2[lengte2.length-1].
>
> Just write this algorithm out in Java and you've solved your homework.

ok but i am sorry to say this but I don't get it :(
can you explain it a little easyer
>
> --
> /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste       W++ B OP+                     |
> \----------------------------------------- Finland rules! ------------/
> "Remember: There are only three kinds of people - those who can count and
those
> who can't."
>    - Vampyra

```
 0
Reply tommaz (9) 9/28/2003 4:03:38 PM

```"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message news:bl707n\$quv\$1@oravannahka.helsinki.fi...
> Tom Wouters <Tommaz@pandora.be> scribbled the following:
> > hi everone,
>
> > i am trying to write a function called shiftDown.
> > This function needs to move the contents of an array one place back .
> > ex. int[] intarray = new int[4]
>
> >         intarray[0] = 1;
> >         intarray[1] = 2;
> >         intarray[2] = 3;
> >         intarray[3] = 4;
>
> > then i want to use the function and i must have the following: intarray[0]
> > must then be 2
> >                intarray[1] must be 3
> >                intarray[2] must be 4
> >         and intarray[3] must be 1
>
> > i am working on this for days now and still can't figure it out    this is
> > what i allready have but it doesn't work
>
> > public void shiftDown() {
> >   for (int i = 0; i < lengte2.length; i++) {
> >   if ( lengte2[i] == lengte2.length) {
> >     lengte2[i] = lengte2[0];
> >     lengte2[i] = lengte2[i + 1];
> >   }
> >   }
> >  }
>
> > lengte2 is my array.
>
> Here's an algorithm for you. Put lengte2[0] into some temporary variable.
> Then for all i in 1 to lengte[2].length-1, put lengte2[i] into
> lengte2[i-1]. Finally put the temporary variable into
> lengte2[lengte2.length-1].
>
> Just write this algorithm out in Java and you've solved your homework.

This is so C-ish (and I love it) but to the OP I want to say two things:

1) Have a look at System.arraycopy() (if you like Joona's solution);
2) Have a look at the ArrayList class.

kind regards,

Jos
```
 0
Reply j.a.horsmeier (104) 9/28/2003 4:14:30 PM

```On Sun, 28 Sep 2003 15:47:15 +0000, Tom Wouters wrote:

> public void shiftDown() {
>   for (int i = 0; i < lengte2.length; i++) {
>   if ( lengte2[i] == lengte2.length) {
>     lengte2[i] = lengte2[0];
>     lengte2[i] = lengte2[i + 1];
>   }
>   }
>  }

Looks like a Computer Science 101 homework problem.
You may be a student, so, I am not going to give you code.

But, here is an explanation of one possible solution:

First, you copy the 0th element into a temporary variable.

Next, you want to loop from the 0th element all the way
to the (n-1)th element and copy the next element's value
into the current element.

Last, you copy the value of the temporary variable into the
nth element.

Do you understand this solution and if it will work?
If so, try to write the code yourself.

Good luck.
```
 0
Reply no.spam4136 (32) 9/28/2003 4:22:06 PM

```Tom Wouters wrote:

>
> "Joona I Palaste" <palaste@cc.helsinki.fi> schreef in bericht
> news:bl707n\$quv\$1@oravannahka.helsinki.fi...
>> Tom Wouters <Tommaz@pandora.be> scribbled the following:
>> > hi everone,
>>
>> > i am trying to write a function called shiftDown.
>> > This function needs to move the contents of an array one place back .
>> > ex. int[] intarray = new int[4]
>>
>> >         intarray[0] = 1;
>> >         intarray[1] = 2;
>> >         intarray[2] = 3;
>> >         intarray[3] = 4;
>>
>> > then i want to use the function and i must have the following:
> intarray[0]
>> > must then be 2
>> >                intarray[1] must be 3
>> >                intarray[2] must be 4
>> >         and intarray[3] must be 1
>>
>> > i am working on this for days now and still can't figure it out    this
> is
>> > what i allready have but it doesn't work
>>
>> > public void shiftDown() {
>> >   for (int i = 0; i < lengte2.length; i++) {
>> >   if ( lengte2[i] == lengte2.length) {
>> >     lengte2[i] = lengte2[0];
>> >     lengte2[i] = lengte2[i + 1];
>> >   }
>> >   }
>> >  }
>>
>> > lengte2 is my array.
>>
>> Here's an algorithm for you. Put lengte2[0] into some temporary variable.
>> Then for all i in 1 to lengte[2].length-1, put lengte2[i] into
>> lengte2[i-1]. Finally put the temporary variable into
>> lengte2[lengte2.length-1].
>>
>> Just write this algorithm out in Java and you've solved your homework.
>
>
> ok but i am sorry to say this but I don't get it :(
> can you explain it a little easyer
>>
>> --
>> /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
>> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
>> | http://www.helsinki.fi/~palaste       W++ B OP+                     |
>> \----------------------------------------- Finland rules! ------------/
>> "Remember: There are only three kinds of people - those who can count and
> those
>> who can't."
>>    - Vampyra
In simple English : put the first element of your array (length2[0] ) into
some temp. variable like ( int temp = length2[0] ), and then do the
following :
For all i in 1 to (length2.length-1), length2[i-1] is set to length2[i].
When this loop finishes, put the temp variable (temp) back into
length2[length2.length-1].
Sorry, but simpler then this it doesn't come.
Brgds, Herman

--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
```
 0

```Dank u  ik denk dat u ofwel nederlands of belgisch bent  kan u misschien
even uitleggen in nederlands aub.

alvast bedankt

ps dit snap ik niet zo goed:  For all i in 1 to (length2.length-1),
length2[i-1] is set to length2[i].

"Herman Timmermans" <timmermansdot.hermanat@skynet.be> schreef in bericht
> Tom Wouters wrote:
>
> >
> > "Joona I Palaste" <palaste@cc.helsinki.fi> schreef in bericht
> > news:bl707n\$quv\$1@oravannahka.helsinki.fi...
> >> Tom Wouters <Tommaz@pandora.be> scribbled the following:
> >> > hi everone,
> >>
> >> > i am trying to write a function called shiftDown.
> >> > This function needs to move the contents of an array one place back .
> >> > ex. int[] intarray = new int[4]
> >>
> >> >         intarray[0] = 1;
> >> >         intarray[1] = 2;
> >> >         intarray[2] = 3;
> >> >         intarray[3] = 4;
> >>
> >> > then i want to use the function and i must have the following:
> > intarray[0]
> >> > must then be 2
> >> >                intarray[1] must be 3
> >> >                intarray[2] must be 4
> >> >         and intarray[3] must be 1
> >>
> >> > i am working on this for days now and still can't figure it out
this
> > is
> >> > what i allready have but it doesn't work
> >>
> >> > public void shiftDown() {
> >> >   for (int i = 0; i < lengte2.length; i++) {
> >> >   if ( lengte2[i] == lengte2.length) {
> >> >     lengte2[i] = lengte2[0];
> >> >     lengte2[i] = lengte2[i + 1];
> >> >   }
> >> >   }
> >> >  }
> >>
> >> > lengte2 is my array.
> >>
> >> Here's an algorithm for you. Put lengte2[0] into some temporary
variable.
> >> Then for all i in 1 to lengte[2].length-1, put lengte2[i] into
> >> lengte2[i-1]. Finally put the temporary variable into
> >> lengte2[lengte2.length-1].
> >>
> >> Just write this algorithm out in Java and you've solved your homework.
> >
> >
> > ok but i am sorry to say this but I don't get it :(
> > can you explain it a little easyer
> >>
> >> --
> >> /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
> >> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> >> | http://www.helsinki.fi/~palaste       W++ B OP+                     |
> >> \----------------------------------------- Finland rules! ------------/
> >> "Remember: There are only three kinds of people - those who can count
and
> > those
> >> who can't."
> >>    - Vampyra
> In simple English : put the first element of your array (length2[0] ) into
> some temp. variable like ( int temp = length2[0] ), and then do the
> following :
> For all i in 1 to (length2.length-1), length2[i-1] is set to length2[i].
> When this loop finishes, put the temp variable (temp) back into
> length2[length2.length-1].
> Sorry, but simpler then this it doesn't come.
> Brgds, Herman
>
> --
> Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
> Anti Spam = remove the "dot" and the "at"
> Registered Linux User #264690

```
 0
Reply tommaz (9) 9/28/2003 4:30:36 PM

```Tom Wouters wrote:
As a matter of exception I'll reply in Dutch for you - sorry for all the
others ;-)
Indien je array bv? 5 elementen heeft [0..4] dan maak je een For loop
constructie (zoals in C) in Java (for i=1 to (5-1) maw 4) en dan zet je
length2[i-1]=length2[i]  (bv. wanneer je in de loop komt, en i dus 1 is,
length2[1-1] (is dus length2[0]) = length2[1], en zo verder tot de loop
stopt.
Mvg, Herman

> Dank u  ik denk dat u ofwel nederlands of belgisch bent  kan u misschien
> even uitleggen in nederlands aub.
>
> alvast bedankt
>
> ps dit snap ik niet zo goed:  For all i in 1 to (length2.length-1),
> length2[i-1] is set to length2[i].
>
> "Herman Timmermans" <timmermansdot.hermanat@skynet.be> schreef in bericht
>> Tom Wouters wrote:
>>
>> >
>> > "Joona I Palaste" <palaste@cc.helsinki.fi> schreef in bericht
>> > news:bl707n\$quv\$1@oravannahka.helsinki.fi...
>> >> Tom Wouters <Tommaz@pandora.be> scribbled the following:
>> >> > hi everone,
>> >>
>> >> > i am trying to write a function called shiftDown.
>> >> > This function needs to move the contents of an array one place back
>> >> > . ex. int[] intarray = new int[4]
>> >>
>> >> >         intarray[0] = 1;
>> >> >         intarray[1] = 2;
>> >> >         intarray[2] = 3;
>> >> >         intarray[3] = 4;
>> >>
>> >> > then i want to use the function and i must have the following:
>> > intarray[0]
>> >> > must then be 2
>> >> >                intarray[1] must be 3
>> >> >                intarray[2] must be 4
>> >> >         and intarray[3] must be 1
>> >>
>> >> > i am working on this for days now and still can't figure it out
> this
>> > is
>> >> > what i allready have but it doesn't work
>> >>
>> >> > public void shiftDown() {
>> >> >   for (int i = 0; i < lengte2.length; i++) {
>> >> >   if ( lengte2[i] == lengte2.length) {
>> >> >     lengte2[i] = lengte2[0];
>> >> >     lengte2[i] = lengte2[i + 1];
>> >> >   }
>> >> >   }
>> >> >  }
>> >>
>> >> > lengte2 is my array.
>> >>
>> >> Here's an algorithm for you. Put lengte2[0] into some temporary
> variable.
>> >> Then for all i in 1 to lengte[2].length-1, put lengte2[i] into
>> >> lengte2[i-1]. Finally put the temporary variable into
>> >> lengte2[lengte2.length-1].
>> >>
>> >> Just write this algorithm out in Java and you've solved your homework.
>> >
>> >
>> > ok but i am sorry to say this but I don't get it :(
>> > can you explain it a little easyer
>> >>
>> >> --
>> >> /-- Joona Palaste (palaste@cc.helsinki.fi)
>> >> ---------------------------\
>> >> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA
>> >> | N+++|
>> >> | http://www.helsinki.fi/~palaste       W++ B OP+
>> >> | |
>> >> \----------------------------------------- Finland rules!
>> >> ------------/ "Remember: There are only three kinds of people - those
>> >> who can count
> and
>> > those
>> >> who can't."
>> >>    - Vampyra
>> In simple English : put the first element of your array (length2[0] )
>> into some temp. variable like ( int temp = length2[0] ), and then do the
>> following :
>> For all i in 1 to (length2.length-1), length2[i-1] is set to length2[i].
>> When this loop finishes, put the temp variable (temp) back into
>> length2[length2.length-1].
>> Sorry, but simpler then this it doesn't come.
>> Brgds, Herman
>>
>> --
>> Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
>> Anti Spam = remove the "dot" and the "at"
>> Registered Linux User #264690

--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
```
 0

```Jos A. Horsmeier <j.a.horsmeier@wanadoo.nl> scribbled the following:
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message news:bl707n\$quv\$1@oravannahka.helsinki.fi...
>> Tom Wouters <Tommaz@pandora.be> scribbled the following:
>> > hi everone,
>>
>> > i am trying to write a function called shiftDown.
>> > This function needs to move the contents of an array one place back .

(snip)

> This is so C-ish (and I love it) but to the OP I want to say two things:

> 1) Have a look at System.arraycopy() (if you like Joona's solution);
> 2) Have a look at the ArrayList class.

Who knows, maybe Tom's professor wants him to give a C-ish solution.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Make money fast! Don't feed it!"
- Anon
```
 0
Reply palaste (2323) 9/28/2003 4:42:08 PM

```Ok i found it out and it worked thanks

but if i want to do this in the other direction(move all the contents of the
arrays one place up instead of down)
then i get an arrayindexoutofboundexception.
I wrote the code like this:

public void shiftUp() {
int temp = lengte2[lengte2.length - 1];
for (int i = 0; i < lengte2.length; i++) {
lengte2[i + 1] = lengte2[i];
}
lengte2[0] = temp;
}

does this means that my for statement keeps on "rolling"?

"Tom Wouters" <Tommaz@pandora.be> schreef in bericht
news:7oDdb.45756\$v93.1894515@phobos.telenet-ops.be...
> hi everone,
>
> i am trying to write a function called shiftDown.
> This function needs to move the contents of an array one place back .
> ex. int[] intarray = new int[4]
>
>         intarray[0] = 1;
>         intarray[1] = 2;
>         intarray[2] = 3;
>         intarray[3] = 4;
>
> then i want to use the function and i must have the following: intarray[0]
> must then be 2
>                intarray[1] must be 3
>                intarray[2] must be 4
>         and intarray[3] must be 1
>
> i am working on this for days now and still can't figure it out    this is
> what i allready have but it doesn't work
>
> public void shiftDown() {
>   for (int i = 0; i < lengte2.length; i++) {
>   if ( lengte2[i] == lengte2.length) {
>     lengte2[i] = lengte2[0];
>     lengte2[i] = lengte2[i + 1];
>   }
>   }
>  }
>
> lengte2 is my array.
>
> TIA
>
> Tommaz
>
>

```
 0
Reply tommaz (9) 9/28/2003 6:37:31 PM

```On Sun, 28 Sep 2003 18:37:31 GMT, Tom Wouters wrote:
> Ok i found it out and it worked thanks
>
> but if i want to do this in the other direction(move all the
> contents of the arrays one place up instead of down) then i get an
> arrayindexoutofboundexception.
>
> I wrote the code like this:
>
> public void shiftUp() {
>   int temp = lengte2[lengte2.length - 1];
>   for (int i = 0; i < lengte2.length; i++) {
>     lengte2[i + 1] = lengte2[i];
>    }
>    lengte2[0] = temp;
>   }
>
> does this means that my for statement keeps on "rolling"?

No, it means that you need to think more about which array positions
you attempt to access.

Remember that the last position in an array is at length-1.

Your loop only goes to length-1, but you access both positions i and
i+1 on each pass, so you go too far.

/gordon

--
[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e
```
 0
Reply not108 (1253) 9/28/2003 6:51:30 PM

```On Sun, 28 Sep 2003 18:37:31 GMT, "Tom Wouters" <Tommaz@pandora.be>
wrote or quoted :

> for (int i = 0; i < lengte2.length; i++) {
>    lengte2[i + 1] = lengte2[i];
>   }
You are overwriting yourself, and just propagating the [0] element
throughout.  To shift up, you must work from the top down.   To shift
down you would work from the bottom up.

See http://mindprod.com/jgloss/jcheat.html for how to do a reverse
for.

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
```
 0
Reply roedy4 (647) 9/28/2003 7:25:10 PM

```ok  now my code is like this but i still get the exception with a -1 after
it.  My for is rolling till -1 but i don't know how to fix this.
can someone help me with this

public void shiftUp() {
int temp = lengte2[lengte2.length - 1];
for (int i = lengte2.length - 1; i >= 0; i--) {
lengte2[i - 1] = lengte2[i];
}
lengte2[0] = temp;
}

Tommaz

"Gordon Beaton" <not@for.email> schreef in bericht
news:3f772dac\$1@news.wineasy.se...
> On Sun, 28 Sep 2003 18:37:31 GMT, Tom Wouters wrote:
> > Ok i found it out and it worked thanks
> >
> > but if i want to do this in the other direction(move all the
> > contents of the arrays one place up instead of down) then i get an
> > arrayindexoutofboundexception.
> >
> > I wrote the code like this:
> >
> > public void shiftUp() {
> >   int temp = lengte2[lengte2.length - 1];
> >   for (int i = 0; i < lengte2.length; i++) {
> >     lengte2[i + 1] = lengte2[i];
> >    }
> >    lengte2[0] = temp;
> >   }
> >
> > does this means that my for statement keeps on "rolling"?
>
> No, it means that you need to think more about which array positions
> you attempt to access.
>
> Remember that the last position in an array is at length-1.
>
> Your loop only goes to length-1, but you access both positions i and
> i+1 on each pass, so you go too far.
>
> /gordon
>
> --
> [  do not email me copies of your followups  ]
> g o r d o n + n e w s @  b a l d e r 1 3 . s e

```
 0
Reply tommaz (9) 9/28/2003 9:01:13 PM

```Tom Wouters wrote:

> ok  now my code is like this but i still get the exception with a -1 after
> it.  My for is rolling till -1 but i don't know how to fix this.
> can someone help me with this
>
> public void shiftUp() {
>   int temp = lengte2[lengte2.length - 1];
>   for (int i = lengte2.length - 1; i >= 0; i--) {
>     lengte2[i - 1] = lengte2[i];
>    }
>    lengte2[0] = temp;
>  }
>
> Tommaz
[knip]
Tom,
it should be for (int i = lengte2.length-1;i>0;i--),
because if i=0, your next statement becomes like this :
length2[0-1] = length2[0]  << this gives the error, a negative index into

Brgds, Herman
--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
```
 0

```Hello,

i am still working on this project.  I am a little bit further now but now i
have to give a parameter to the methode shiftDown so that i can say how many
place it has to shift down. My code is like this but i still get an
arrayindexoutofboundexception. I don't know why because i wrote it down on a
paper and it seems to be right.

public void shiftDown(int hoeveel) {

for (int i = 1; i <= lengte2.length; i++) {

if (lengte2[i + hoeveel - 1]>=lengte2.length){

lengte2[i - 1] = lengte2[i + hoeveel - 1 - lengte2.length];

}else{

lengte2[i - 1] = lengte2[i + hoeveel - 1];

}

}

}

greets Tom

"Tom Wouters" <Tommaz@pandora.be> schreef in bericht
news:7oDdb.45756\$v93.1894515@phobos.telenet-ops.be...
> hi everone,
>
> i am trying to write a function called shiftDown.
> This function needs to move the contents of an array one place back .
> ex. int[] intarray = new int[4]
>
>         intarray[0] = 1;
>         intarray[1] = 2;
>         intarray[2] = 3;
>         intarray[3] = 4;
>
> then i want to use the function and i must have the following: intarray[0]
> must then be 2
>                intarray[1] must be 3
>                intarray[2] must be 4
>         and intarray[3] must be 1
>
> i am working on this for days now and still can't figure it out    this is
> what i allready have but it doesn't work
>
> public void shiftDown() {
>   for (int i = 0; i < lengte2.length; i++) {
>   if ( lengte2[i] == lengte2.length) {
>     lengte2[i] = lengte2[0];
>     lengte2[i] = lengte2[i + 1];
>   }
>   }
>  }
>
> lengte2 is my array.
>
> TIA
>
> Tommaz
>
>

```
 0
Reply tommaz (9) 9/29/2003 6:54:05 PM

```Tom Wouters wrote:

> Hello,
>
> i am still working on this project.  I am a little bit further now but now
> i have to give a parameter to the methode shiftDown so that i can say how
> many place it has to shift down. My code is like this but i still get an
> arrayindexoutofboundexception. I don't know why because i wrote it down on
> a paper and it seems to be right.
>
> public void shiftDown(int hoeveel) {
>
> for (int i = 1; i <= lengte2.length; i++) {
>
> if (lengte2[i + hoeveel - 1]>=lengte2.length){
>
> lengte2[i - 1] = lengte2[i + hoeveel - 1 - lengte2.length];
>
> }else{
>
> lengte2[i - 1] = lengte2[i + hoeveel - 1];
>
> }
>
> }
>
> }
>
>
>
> greets Tom
>
> "Tom Wouters" <Tommaz@pandora.be> schreef in bericht
> news:7oDdb.45756\$v93.1894515@phobos.telenet-ops.be...
>> hi everone,
>>
>> i am trying to write a function called shiftDown.
>> This function needs to move the contents of an array one place back .
>> ex. int[] intarray = new int[4]
>>
>>         intarray[0] = 1;
>>         intarray[1] = 2;
>>         intarray[2] = 3;
>>         intarray[3] = 4;
>>
>> then i want to use the function and i must have the following:
>> intarray[0] must then be 2
>>                intarray[1] must be 3
>>                intarray[2] must be 4
>>         and intarray[3] must be 1
>>
>> i am working on this for days now and still can't figure it out    this
>> is what i allready have but it doesn't work
>>
>> public void shiftDown() {
>>   for (int i = 0; i < lengte2.length; i++) {
>>   if ( lengte2[i] == lengte2.length) {
>>     lengte2[i] = lengte2[0];
>>     lengte2[i] = lengte2[i + 1];
>>   }
>>   }
>>  }
>>
>> lengte2 is my array.
>>
>> TIA
>>
>> Tommaz
>>
>>
Tom,
I think we all like to help, but you will not be learning Java by letting
other people solve your homework for you....
Question : Can the shiftdown integer parameter be larger then the size of
the array?
Question : if it can be larger, should you do a full rotate ?
Brgds,
Herman
*********************************
--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
```
 0

15 Replies
41 Views

Similar Articles

12/13/2013 1:19:36 AM
[PageSpeed]

Similar Artilces:

hi all i have simple Problem please tell me the Solution if u know?? main() { int a[3][3]={1,2,3,4,5,6,7,8,9}; printf("%u %u %u",a[0],a[1],a[2]); } when i execute this program i am getting a Fixed address Value(multi digit value) can any body explain me ahy this is giving the same value for different index Thanks In Advance -Sachin sachinv1821@gmail.com wrote: > hi all i have simple Problem please tell me the Solution if u know?? > main() > { > int a[3][3]={1,2,3,4,5,6,7,8,9}; > printf("%u %u %u",a[0],a[1],a[2]); > } > when i execute this program i a...

simple prob passing a reference of arrays to a func.
void DoThang (char (&cMovesDone)[9]) { for(int i = 0; i<9; i++) cMovesDone[i] = 'k'; } int main() { char c; DoThang (c); cout<<c; } I tried outputting the cMovesDone using cout<<. It's displaying wierd characters. what's wrong? MC felon wrote: > void DoThang (char (&cMovesDone)[9]) > { > for(int i = 0; i<9; i++) > cMovesDone[i] = 'k'; > } > > int main() > { > char c; > DoThang (c); > cout<<c; > } > > I tried outputting the cMovesDone using cout<<. It's displaying wierd >...

const and array of array (of array ...)
Imagine I have an array of arrays of ints and want to sum all the ints. #include <stdio.h> int sumints(int arr[3][3]) { int c, r, s=0; for (r=0; r<3; r++) { for (c=0; c<3; c++) { s += arr[r][c]; } } return s; } int main(void) { int my_array[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; printf("sum of ints is %d\n", sumints(my_array)); return 0; } I tried to define the su...

array of array
Hi, I need to create something like a matrix, say 32 bits on each row and up to 4 rows. That's why on my Spartan3 I have to receive 32 bits word from a serial port and I have to store them (I know I can just place a 32x4 bits long vector and store the data in the same row, but I'd like to learn more about array of array). I wrote: type row is array (cmd_buffer_word downto 0) of std_logic; type cmd_buffer is array (cmd_buffer_depth downto 0) of row; now, how could I transfer the whole 32 bits data inside a vector called cmd_tmp_buffer into the second row of my cmd_buffer? This follow...

array of array
I've to save in an array of array some values. Problem: I don't know the size of the array before filling it. I'd like something like \$bigarray = array(): then runnig the code I must create dinamically (I don't know how many) few arrays in the bigarray; \$bigarray["X"] = array(): \$bigarray["Y"] = array(): then fill those arrays with few unknow number of values. array_push(\$bigarray["X"],\$value1); array_push(\$bigarray["X"],\$value2); array_push(\$bigarray["Y"],\$value5); .... but array_push doesn't manage it. How to do so...

arrays of arrays question
ok, i have been thru perlfaq4 (i believe four) and my manuals aren't helping me either, and/or it's just 3am...but need some help syntax wise. in brief, i am going through my database afetr a query. so what i am attempting to do, is to make an array of each array. now below is what i have currently, but i can't figure out how to get OUT each array. i tried a POP, but that didn't seem to give me an array i could access (i.e. \$row[0], etc). now i do realize that \@row, means the address. so am curious if that means that the reference to each row is cleared. but when i do the...

combine array of array
\$array1 = array( array( "product_id" => 3, "SKU" => "AES46", "qty" => 3, ), ); \$array2 = array( array( "product_id" => 3, "SKU" => "AES46", "qty" => 2, ), array( "product_id" => 4, "SKU" => "AES47", "qty" => 3, ), ); I want to add up the qty of two array if the product_id is same I try on \$array1 + \$array2, but which is not I want, I only have to use 2 foreach loop in my mind foreach ...

Reconfigurable Array of Array
just for fun I wrote down how I see it possible to have different FPGAs to be cut out from single wafer covered with completly repeating pattern http://help.xilant.com/RAA I am not including the text here as I may edit the original even before the post is appearing in the NG -- Antti Lukats http://www.xilant.com Antti Lukats wrote: > just for fun I wrote down how I see it possible to have different FPGAs to > be cut out from single wafer covered with completly repeating pattern > > http://help.xilant.com/RAA > > I am not including the text here as I may edit the ...

How to define array of array
Hello everyone, How to define an array like this: array myarr = ( US => (company1 => Tel1, company2 => Tel2, company3 => Tel3), JP => (company1 => Tel1, company2 => Tel2), CO => (company1 => Tel1, company3 => Tel3) ); Thanks. On Nov 27, 8:13 pm, tju329 <tju...@gmail.com> wrote: > Hello everyone, > > How to define an array like this: > > array myarr = ( > US => (company1 => Tel1, company2 => Tel2, company3 => Tel3), > JP => (company1 => Tel1, company2 => Tel2), > CO => (company1 => Tel1, co...

Making an array of arrays?
Hello, I'm trying to make an array of arrays, but my code is not behaving as I expect. Specifically I am trying to make an array to hold Sudoku boards (each board is a 2D array). I am doing this because I am trying to use genetic programming techniques to attempt to solve a simple sudoku puzzle. Here is the code I am using to copy the @board array in to @offspring: #When the for loop can take the value from pop must subtract 1 from it to avoid having to do nasty index manipulation on the array i = 0 #Define an offspring array that will hold the copies of the @board array @offspring...

Associate array of arrays
Pardon my ignorance here... I have created arrays such as: var SDET_Lab130= new Array(); SDET_Lab130['SDET'] = new Array(); SDET_Lab130['SDET'][0] = [0,400,7,24431,25179,24919,1.09,base]; SDET_Lab130['SDET'][1] = [1,400,7,27729,28031,27877,0.34,11.87,rate Improved]; SDET_Lab130['SDET'][2] = [0,450,7,24691,25194,24966,0.75,base]; SDET_Lab130['SDET'][3] = [1,450,7,27598,28090,27860,0.60,11.59,rate Improved]; SDET_Lab130['SDET'][4] = [0,500,7,24725,25171,24967,0.74,base]; SDET_Lab130['SDET'][5] = [1,500,7,27556,27941,27795,0.52,11.32,rate I...

how to join array into array
Hi I am having two array example below : @x; @y; values in x and y are : \$x[0] = (1,2,3,4) \$x[1] = (2,3,4,5) ..... till \$x[31] = (1,1,1,1) now the value of y contains \$y[0] = (2,2,3,4) \$y[1] = (2,3,3,5) ..... till \$y[31] =(4,4,4,4) ...... till \$y[500] = (1,1,1,1) i want to make a loop where i can append the array y or create new array with having array x value fixed till record [31] ie end of array x plus value of array[y] till record [31] and then repeating again the value of array x to next records of array y i mean till next 31 records of y so if you see the results output it shoul...

array of arrays question
Hi, Here is what I'm doing in python 2.4.1 : >>> aOfa=[[[]]*3]*4 >>> aOfa [[[], [], []], [[], [], []], [[], [], []], [[], [], []]] >>> aOfa[3][2].append(1) >>> aOfa [[[1], [1], [1]], [[1], [1], [1]], [[1], [1], [1]], [[1], [1], [1]]] Ok, so there is something I didn't understand about python's arrays. The result I was expecting was : [[[], [], []], [[], [], []], [[], [], []], [[], [], [1]]] Somebody understand what's going on here? Thank "Meo" <eric.paquet@isrec.ch> writes: > Somebody understand what's going on...

Repacking an array of arrays
Is there an elegant (maybe a one-liner) for repacking: [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]] to? [[1, 4, 7], [10, 13, 2], [5, 8, 11], [14, 3, 6], [9, 12, 15]] So rather than having 3 arrays each with 5 elements, I get five arrays of 3 elements each... the only way I can see to do this is by looping the array, and creating new arrays. Any help would be much appreciated. -- Posted via http://www.ruby-forum.com/. Hi -- On Tue, 10 Jul 2007, Kaps Lok wrote: > Is there an elegant (maybe a one-liner) for repacking: > > [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3...

length of an array in a struct in an array of structs in a struct in an array of structs
Hello, So I have this complex data structure I've built for myself, and I'm trying to get this snippet to work: \$k < scalar(@{@{@VisitArray[\$i]->PSearchStructArray[\$j]->ClickPathArray}}) VisitArray is an array of VisitStructs. Each VisitStruct contains an array of PSearchStructs called PSearchStructArray. Each PSearchStruct contains an array called ClickPathArray. I'm trying to get the length of ClickPathArray out. I've tried numerous variations of that line, and none of them work. I do know that scalar(@{@VisitArray[\$i]->PSearchStructArray}) will return the...

looping through array of associative arrays
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new Array(); teams["team1"] = "Lakers"; teams["score1"] = "78"; teams["team1"] = "Sacramento"; teams["score2"] = "88"; games[0] = teams; var teams = new Array(); teams["team1"] = "Houston"; teams["score1"] = "94"; teams["team1"] = "Dallas"; teams["score2"] ...

Sorting an array by another array
I want to sort an array by the order the elements appear in a second array. For example suppose I have: \$array1 = qw ( item-a item-b ); \$array2 = qw ( item-b3 item-a2 ); I would like to sort array2 by array1 so array2 becomes: item-a3, item-b2 Is there any easy way to do it withouth brute force way of comparing every element in array2 with every element in array1? el_roachmeister@yahoo.com wrote: > I want to sort an array by the order the elements appear in a second > array. For example suppose I have: Give real data, it makes talking about it easier. > \$array1 = qw ( item...

Array#first is not Array#[0]
I made a class that is a subclass of Array : class MyClass < Array def [](i) "Here you go: #{super(i)}" end end and I thought that Array#first and Array#last was just syntactic sugar that would translate to Array#[0] and Array#[-1] respectively. But it's not: irb> g = MyClass.new([1,2,3]) => [1, 2, 3] irb> g[0] => "Here you go: 1" irb> g.first => 1 Now, isn't that really, really crazy? Am I supposed to define MyClass#first and MyClass#last separately??? Best regards, Fredrik -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Fredrik wr...

lost in an array of array references...
please help.. im just now getting into references and im so very lost. the following code use strict; use warnings; my @src_dbs = qw(a b c); # add more dbs to compare to here my @data_src; foreach (@src_dbs){ print "getting data from db \$_\n"; push @data_src, &get_db_data(\$_); } print "\nthere are ",scalar(@data_src)," arrays\n"; print "\narray one has ",scalar(\$data_src[0])," items\n"; # <<< __i think this is my problem, but how should i refer to this array to get the # of items correctly ? foreach (@data_src){ print "...

Printing Hash of Array of Arrays
Hi All, How to print hash of array of arrays without using Data::Dumper. I am trying to print %hash = ( FILE1 => [ [ STATE1 STATE2 STATE3 ], [ STATEX STATEA STATE4 ] ], FILE2 =&...