Fill / Delete array with values from methods

I am a real newbie at working with arrays...:

I would like to fill an array with values that are returned by some of my methods. The first index should be a continuous number (an "ID"):

privateint[] storedReturns;

The array should have the following structure:

storedReturns = {ID, getSpeed(), getDistance(), getTime()}

With every mouse click I would like to add new values:

storedReturns = {1, 20, 40, 100},

{2, 30, 20, 40},

{3, 40, 50, 20},

etc.

publicvoid mouseClicked(MouseEvent evt){

if (SwingUtilities.isLeftMouseButton(evt)

storedReturns...?

How can I fill my array with these values? How do I delete a a complete "row"/"line"?

Thanks!

[929 byte] By [SFLa] at [2007-11-27 11:07:34]
# 1

Read this tutorial about array use, then ask specific questions if you encounter any problems.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

ChuckBinga at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks! So I have 2 choices:

public void mouseClicked(MouseEvent evt) {

if (SwingUtilities.isLeftMouseButton(evt)

int id++;

storedReturns[0] = id;

storedReturns[1] = getSpeed();

storedReturns[2] = getDistance();

storedReturns[3] = getTime();

... or the following:

public void mouseClicked(MouseEvent evt) {

if (SwingUtilities.isLeftMouseButton(evt)

int id++;

storedReturns[] = {ID, getSpeed(), getDistance(), getTime()};

Could I store int, double, Point2D,... values together in 1 array?

SFLa at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 3

Arrays can only hold Objects (ints, double are not objects they are primitives).

If you have several different types in an array you have to make the

array type the super class of all the different Objects - which

usually is just Object.

Object[] myArray = new Object[]{ new Point(), new Double(), new String{} }

TuringPesta at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 4

e.g.:

private Object[] storedReturns = new Object[]{new Double(), new Point2D(), new Double()};

gives: "The constructor Double() is undefined".

Why?

SFLa at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 5

do you know about wrapper classes in java? if not that is what new Double(), new Integer() is. learn about autoboxing also in java 5

lrngjavaa at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 6

> Arrays can only hold Objects (ints, double are not

> objects they are primitives).

You must have a different Java book than me. I have no problems with this.

int[] numbers = {1,2,3,4,5};

floundera at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 7

> You must have a different Java book than me. I have

He meant to say Collections, didn't he?

petes1234a at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 8

> Arrays can only hold Objects (ints, double are not

> objects they are primitives).

Erm, no. Arrays can most definitely hold primitives. You may not have meant to say what you said.

> If you have several different types in an array...

One should be cautious about this. Different types should be stored in an array typically only when the array references only need to be of the most general type; i.e., the Array's declared type.

> have to make the array type the super class of all the different

> Objects - which usually is just Object.

I'm not sure about that statistic.

~

yawmarka at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 9

OK, I guess it's enough for me just to store double values.

Here is how I initialise my array:

private double[] storedReturns;

private double arrayID = 1;

With every left-mouse click I would like to add at index 0 a continuous ID, then some values I get from my methods:

public void mouseClicked(MouseEvent evt) {

if (SwingUtilities.isLeftMouseButton(evt)

arrayID++;

storedReturns[0] = arrayID; // error

(...)

However, I get a java.lang.NullPointerException although I increase arrayID...? How to solve this? Thanks for your help!

SFLa at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 10

Wouldn't it be easier if the OP just created his own class to contain the variables he wants to store?

That way he won't need to worry about what order they are etc?

i.e.

class MyObject {

private int myVar1;

private double myVar2;

private String myVar3;

/** Getter and Setter Methods here **/

}

c0demonk3ya at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 11

I already solved my previous problem. I am now storing the values in the following way:

arrayID++;

storedReturns[0] = arrayID;

storedReturns[1] = speed;

storedReturns[2] = distance;

storedReturns[3] = time;

However, I think I have to use an ArrayList instead of an Array... Do you know a good tutorial? I didn't found a good explanation in the Sun Java tutorial.

SFLa at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 12

> I already solved my previous problem. I am now

> storing the values in the following way:

> > arrayID++;

> storedReturns[0] = arrayID;

> storedReturns[1] = speed;

> storedReturns[2] = distance;

> storedReturns[3] = time;

>

You should not be doing it this way. You should create a class whos fields are speed, distance, etc.

jverda at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 13

Hmmm... I am currently trying to save the returns from my methods in an ArrayList:

private List<Double> storedReturns = new ArrayList<Double>();

private double arrayID;

However, I have one problem:

storedReturns.add(arrayID, speed);// error

error = "The method add(int, Double) in the type List<Double> is not applicable for the arguments (double, double)"

Why int? I specify arrayID as double...?

SFLa at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 14

Don't put values representing different but related things into an array or collection. Put them into a class.

jverda at 2007-7-29 13:22:54 > top of Java-index,Java Essentials,Java Programming...
# 15

> error = "The method add(int, Double) in the type

> List<Double> is not applicable for the arguments

> (double, double)"

>

> Why int? I specify arrayID as double...?

The method requires (int, Double) and you're giving it (double, double).

WTF are you even trying to do here?

jverda at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 16

I need to save return values from some of my methods for later use. That's all. I thought that an ArrayList would be a good choice for this.

I just need to save these return values and compare them later to inputs from the user. I have to save returns (double) from 5 methods.

How would I save the returns in a class?

SFLa at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 17

> I need to save return values from some of my methods

> for later use. That's all. I thought that an

> ArrayList would be a good choice for this.

It's not. Define a class that holds those related values.

> How would I save the returns in a class?

http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

jverda at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 18

> http://java.sun.com/docs/books/tutorial/java/javaOO/in

> dex.html

Hmmmm.... I know how to write my own classes, and I already needed this for my project. But why should I use a class to store variables? I never have done this. Could you provide an example?

SFLa at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 19

public class Person {

private String name;

private Date birthdate;

// Add get/set methods...

}

Person[] people = new Person[10]

for (int ix = 0 ; ix < people.length; ix+) {

Person person = new Person();

person.setName(some string that we get from somewhere);

person.setBirthdate(some date that we get from somwhere);

}

vs.

String[] names = new String[10];

Date[] birthdates = new Date[10];

for (int ix = 0; ix < names.length; ix+) {

names[ix] = (some string that we get from somewhere);

birthates[ix] = (some date that we get from somewhere);

}

The first one lets you deal with a person as a single entity--a Person object. The second way requires you to keep the names array and birthdate array in sync and deal with them in tandem to work with a single person.

jverda at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 20

Thanks for the example! I choose the 2nd method for my project since it's not that complex and I can keep the variables in sync.

You gave me the example about how to initialize array variables:

String[] names = new String[10];

Date[] birthdates = new Date[10];

I need values in double format without having a fixed size:

private double[] calculatedSpeed= new double[];// error

error = "Variable must provide either dimension expressions or an array initializer"

I can't set a size since I don't know it at program launch... how to solve this?

Thanks for your patience :)

SFLa at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 21

> Thanks for the example! I choose the 2nd method for

> my project since it's not that complex and I can keep

> the variables in sync.

Doh! Just because your program is not that complex doesn't mean that you should get in the habit of doing it that way.

There have been multiple posts from people (including myself) who said you should set up a class to store your variables and perform any manipulation on them.

If your going to do it another way we have all advised against then I wouldn't expect much more help because if you run into any more problems you will simply be told you should be doing it in a class. >:(

c0demonk3ya at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 22

> Doh! Just because your program is not that complex

> doesn't mean that you should get in the habit of

> doing it that way.

Oh, it's just for this one project. I read about the option using a class which you and other users recommended - and which I appreciate very much! For this project I just have to use very very very few data, so I thought it would be easier not to use one more class.

Well, I also tried the way of using a class but I ran into the same problem (see posting #19 by jverd):

Person[] people = new Person[];//error

error = "Variable must provide either dimension expressions or an array initializer".

I don't know how much data I have to store so I can't provide a size. What to do?

SFLa at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 23

> Thanks for the example! I choose the 2nd method

That's the wrong way to do it.

jverda at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 24

> Well, I also tried the way of using a class but I ran

> into the same problem (see posting #19 by jverd):

> > Person[] people = new Person[];//error

>

> error = "Variable must provide either dimension

> expressions or an array initializer".

>

> I don't know how much data I have to store so I can't

> provide a size. What to do?

Use an ArrayList() or LinkedList() instead of a basic array. Then you don't need to worry about the intial size.

c0demonk3ya at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 25

I already changed my code to use the class-method. I still have the problem with the size of the array:

Person[] people = new Person[];//error

SFLa at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 26

> Person[] people = new Person[];//error

> [/code]

> error = "Variable must provide either dimension

> expressions or an array initializer".

>

> I don't know how much data I have to store so I can't

> provide a size. What to do?

You must be supplying a size for your two parallel arrays--10 in your example. It's the same size you'd provide here.

If you really don't know the size, then use a List.

http://java.sun.com/docs/books/tutorial/collections/

jverda at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 27

> If you really don't know the size, then use a List.

private List<Double> storedReturns = new ArrayList<Double>;// error

See my posting #13. I am getting confused...

SFLa at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 28

No no no no no... we told you to use a class

i.e.

private List<Person> storedReturns = new ArrayList<Person>();

c0demonk3ya at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 29

> > If you really don't know the size, then use a

> List.

>

> > private List<Double> storedReturns = new

> ArrayList<Double>;// error

>

>

> See my posting #13. I am getting confused...

Okay, look.

Slow down.

Think about what you are doing.

Don't just throw random bits of code at the wall and see what sticks.

Here you forgot the ().

In reply 13 you tried to use a non-existent method.

You don't want a List of Double. You want a List of the class that you define.

jverda at 2007-7-29 13:22:58 > top of Java-index,Java Essentials,Java Programming...
# 30

Oh, great :) I didn't know that I can use a List that way! Thanks!

I am not sure about the interaction with the Person-class (let's use the example above)... I have this class with getters/setters:

public class Person {

private String name;

private Date birthdate;

(...)

public double getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

Let us assume I have a method calculating the birthday of persons and I would like to store the last 3 calculated birtdays:

public Date calculateBirtday() {

//some calculations

return birthday;

}

How do I exactly save the, e.g., last 3 birthdays in the "Person" class?

I searched for this in the Sun Java Tutorial but found only the chapter "Interfaces" -> "The List Interface", talking about ArrayList and LinkedList... and yes, I have to buy a new Java Book, my current one keeps shtum about this.

SFLa at 2007-7-29 13:23:03 > top of Java-index,Java Essentials,Java Programming...
# 31

> [/code]

> Let us assume I have a method calculating the

> birthday of persons and I would like to store the

> last 3 calculated birtdays:

> > public Date calculateBirtday() {

>//some calculations

> return birthday;

> }

>

> How do I exactly save the, e.g., last 3 birthdays in

> the "Person" class?

You probably don't.

The class that wants the last three bdays gets the last three Persons from the list, call getBday on each one, and stores the results in three variables or an array of size three.

Note that I'm just going along with this since this is the example we're using. I don't know why you'd want the b-days of the last three people in the list, or the somethings of the last three whatevers that you're actually creating. I think you may still have problems expressing--even in your own mind or in English--what you're trying to store and accomplish.

jverda at 2007-7-29 13:23:03 > top of Java-index,Java Essentials,Java Programming...
# 32

jverd, thanks a lot for your help!

I need to store returns from my methods to use them in later calculations.

Could you maybe give me an example about how to store some values using your example above? Let us assume we would like to store the birthday values 1978, 1979 and 1980... :)

Thanks a million!

SFLa at 2007-7-29 13:23:03 > top of Java-index,Java Essentials,Java Programming...