The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with Arrays

2 replies on 1 page. Most recent reply: Nov 14, 2003 10:57 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
john

Posts: 1
Nickname: mick
Registered: Nov, 2003

Help with Arrays Posted: Nov 13, 2003 5:11 AM
Reply to this message Reply
Advertisement
I'm new to Java and trying to figure out how to modify arrays. This is what I made so far:

public class ArrayX {
public static void main(String[] args) {
double[] myArrayX;
myArrayX = new double[8];

// assigns a value to each array element and print
for (int i = 0; i < myArrayX.length; i++) {
myArrayX = i;
myArrayX[0] = 16.0;
myArrayX[1] = 12.0;
myArrayX[2] = 28.0;
myArrayX[3] = 7.0;
myArrayX[4] = 2.5;
myArrayX[5] = 12.0;
myArrayX[6] = 14.0;
myArrayX[7] = -54.5;

System.out.println(myArrayX + " ");
}
System.out.println();
}
}
I'm trying to do the following but I can't find any resources on What exactly to write to do this:

1.Copy the 5th element into the last one
2. Subtract the 1 element from the 4th one and put it in the 5th element
3. sum of the first 5 elements

Now I'm not looking for anybody to do it for me but more to point me in the direction of how the code is supposed to be written to do this. Any help is greatly appreciated!


Tanya

Posts: 16
Nickname: seabeck
Registered: Nov, 2003

Re: Help with Arrays Posted: Nov 14, 2003 12:48 AM
Reply to this message Reply
1.Copy the 5th element into the last one

Array's are very similar to other data types as far as assingments and mathmatical operations go. The only difference is you have to work with the indicies. So when they say to copy the x element of an array into the y element of an array you just go Array[y] = Array[x] (using the assignment operator "=").

2. Subtract the 1 element from the 4th one and put it in the 5th element

This is the same as above but now you have 3 variables very similar to this: x = (z - y) remembering to use the indicies.

3. sum of the first 5 elements

This is just basic addition since you are using ints you can either create a temp variable or create a private int variable and then you say temp = x[0] + x[1] +...+x[n-1]

Hint: just remember when working with arrays many times what will throw you off is what is commonly called off by one error. The reason for this is because arrays start at 0 rather than 1.

For example: for (int i = 0; i < myArrayX.length; i++)
For this statement you might get an array out of bounds error i should be < myArrayX.length - 1. (if you are in fact using i as the index of your arrayX of size 8 and starting i out at 0 (which is correct).

Hope this helps.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help with Arrays Posted: Nov 14, 2003 10:57 AM
Reply to this message Reply
Two more things:

The myArrayX = i; was probably an accident, but just for clarification, since myArrayX was declared as an array of double, this doesn't make any sense. You could assign myArrayX to refer to another array of double, or to null, but not to a single integer (or even float) value. Of course, there is no problem with assigning elements of of the array (as you do later on), eg. myArray[1] = 1.0;.

> This is just basic addition since you are using ints you
> can either create a temp variable or create a private int
> variable and then you say temp = x[0] + x[1] +...+x[n-1]

It looks like it was doubles actually, so you'll need a double for the sum. (Ignoring for the sake of simplicity the fact that when summing a particular data type, it is usually safer to use a larger data type for the sum (eg. sum ints into a long or a BigInteger), to avoid overflow problems).

Flat View: This topic has 2 replies on 1 page
Topic: ARGHHHH THIS IB DOSSIER BYTES!!! Previous Topic   Next Topic Topic: Bruce Eckel's not so simple test

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use