array prob

T

Tom Wouters

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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = lengte2[i + 1];
}
}
}

lengte2 is my array.

TIA

Tommaz
 
J

Joona I Palaste

Tom Wouters said:
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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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 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 ([email protected]) ---------------------------\
| 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
 
T

Tom Wouters

Joona I Palaste said:
Tom Wouters said:
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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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 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
 
J

Jos A. Horsmeier

Joona I Palaste said:
Tom Wouters said:
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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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 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
 
S

Sajjad Lateef

public void shiftDown() {
for (int i = 0; i < lengte2.length; i++) {
if ( lengte2 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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.
 
H

Herman Timmermans

Tom said:
Joona I Palaste said:
Tom Wouters said:
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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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 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 ([email protected]) ---------------------------\
| 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.
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
 
T

Tom Wouters

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.

Herman Timmermans said:
Tom said:
Joona I Palaste said:
Tom Wouters <[email protected]> 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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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 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 ([email protected]) ---------------------------\
| 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.
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
 
H

Herman Timmermans

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 (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.

Herman Timmermans said:
Tom said:
"Joona I Palaste" <[email protected]> schreef in bericht
Tom Wouters <[email protected]> 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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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 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 ([email protected])
---------------------------\
| 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.
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
 
J

Joona I Palaste

Jos A. Horsmeier said:
(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 ([email protected]) ---------------------------\
| 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
 
T

Tom Wouters

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;
}
lengte2[0] = temp;
}

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

Gordon Beaton

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;
}
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
 
T

Tom Wouters

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;
}
lengte2[0] = temp;
}

Tommaz


Gordon Beaton said:
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;
}
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
 
H

Herman Timmermans

Tom said:
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;
}
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
your array

Brgds, Herman
 
T

Tom Wouters

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
 
H

Herman Timmermans

Tom said:
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 said:
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 == lengte2.length) {
lengte2 = lengte2[0];
lengte2 = 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
*********************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top