moving array between classes

Hello there. Right, i have 3 classes with which i am having a bit of trouble with. The first class, "Drawing," creates an array called componentList. Then, in another class called "DrawingCanvas" i have an instance of "Drawing" created. In this second class i have two methods which need to use the array from the first class. These methods are called from a third class, "DrawingFrame."

My problem is that i do not know how to pass the array from "Drawing" into "DrawingCanvas" when i call a method from "DrawingFrame."

Any help is appreciated.

[565 byte] By [chrissmith51a] at [2007-11-26 17:12:59]
# 1

public class C1 {

public void foo() {

Stuff[] arrayOfStuff = new Stuff[1];

C2 c2 = new C2():

arrayOfStuff[0] = new Stuff();

c2.bar(arrayOfStuff);

}

}

public class C2 {

public void bar(Stuff[] arrayOfStuff) {

// do something with arrayOfStuff

}

}

Note that, as with all other objects in Java, you're never passing the array. Rather, you're passing a copy of a reference to the array.

jverda at 2007-7-8 23:40:53 > top of Java-index,Java Essentials,New To Java...
# 2

Do you know how to pass an int or String between classes? if so then you already know how to pass an array. This works for an array of ints

public void iExceptArraysFromOtherClasses(int [] ints)

{

}

kikemellya at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 3

> > public class C1 {

>public void foo() {

>Stuff[] arrayOfStuff = new Stuff[1];

>C2 c2 = new C2():

>arrayOfStuff[0] = new Stuff();

> c2.bar(arrayOfStuff);

>}

>

>

>

> public class C2 {

>public void bar(Stuff[] arrayOfStuff) {

>// do something with arrayOfStuff

> }

> }

> Note that, as with all other objects in Java, you're

> never passing the array. Rather, you're passing a

> copy of a reference to the array.

Sorry, I missed your post.

kikemellya at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 4

despite understanding your replys, i am still having trouble. i think it is because i am trying to put the array into an actionPerformed method. below is the method where i need to pass the array into:

public JMenuItem createFileSaveItem() {

JMenuItem item = new JMenuItem("Save Current Drawing"); // Item name

class MenuItemListener implements ActionListener {

public void actionPerformed(ActionEvent event) { // Event when item clicked

test.saveArray(componentList);

}

}

ActionListener listener = new MenuItemListener();

item.addActionListener(listener);

return item;

}

the "test.saveArray(componentList)" is showing as an error. (cannot find symbol componentList)

at the top of this class i have put:

DrawingCanvas test = new DrawingCanvas();

chrissmith51a at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 5

It is because you don't have componentList in the class you passing from. you need to create it first and then pass it in. is this inner class in the same class file your passing too? if so then you should have access to it's variables if not then you need to make array, set values then pass in.

kikemellya at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 6

ok i think i'm nearly there, now i have:

public void actionPerformed(ActionEvent event) { // Event when item clicked

DrawingCanvas dc = new DrawingCanvas();

Drawing d = new Drawing();

dc.saveArray(d.getArray());

}

This seems to be error free, apart from its creating a NEW drawingCanvas instance, and so it is saving an empty array (i think) I need to use a previously created instance of DrawingCanvas, called from the constructor in DrawingFrame

chrissmith51a at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 7
sorted - thanks for your help
chrissmith51a at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 8
Well if your array begins with your event then it should be created there and passed ereywhere else. It seems your creating in your other class that you dont need. create it in your sction listener and then pass in to a mehtod in your other class. then say the argument passed in = new
kikemellya at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 9

Your code will not work this way. You are creating a new array as you thought (and a new class). do you need to add to an existing array? if you then you need an array list. arrays size can not be changed once the size is specified. when the event is triggered call a method in the other class which excepts the value type of the array, inside this method call the add method of the arrayList. Your current approach is flawed.

kikemellya at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...
# 10
You really shouldn't be passing data around like that anyway. If there are two operations that act on the same data, they should be in the same class. A redesign is what you really need to do.
paulcwa at 2007-7-8 23:40:54 > top of Java-index,Java Essentials,New To Java...