• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

java help pls!

Bacardi151

Senior member
i asked this earlier today, and i haven't had much luck so far

basically i need to know how to pass an array of objects from one CLASS to another CLASS

i have a CLASS called TEST, and another CLASS called SET

i'll write out the CLASS TEST for better visualization:

public class Tester {
public static void main(String[] args) {

String a = "abe";
String b = "ball";
Float c = new Float(3.1832);
String d = "doll";
Float e = new Float(2.50);
int size;

Object[] A = {a, b, c};
Object[] B = {a, b, c, d, e};
size = A.length + B.length;
Set[] C = new Set[size];

C = A.fun(B) // fun is a method written in class Set which i'll now write

...

public class Set {

private Set[] C;
private int size;
private Float checker = new Float(11.82);

public Set() {
}

public Set[] fun(Set[] A, Set[] B) {
size = A.length + B.length;
C = new Set[size];

System.arraycopy(A, 0, C, 0, A.length);
System.arraycopy(B, 0, C, A.length, B.length);

B[1] = A[2];


return C;

}
}

++++++

ok so the general question here is, did i declare the arrays in Test class and Set class correct? (i'd assume not because i keep getting errors). where am i doing wrong? what type should i declare arrays A, B, and C to, in order to use the following: C = A.fun(B);

all help appreciated!!
 
you are probably better off asking in the programming forum... but it looks like A is not an instance of your Set class. You declare A as an array of Objects, so you can't call a method declared for an instance of Set on it.

Some other points that I noticed from quickly looking at your code:

I don't think you want C to be an array of Sets. It should just be Set C = new Set();

your declaration of "fun" in Set is expecting two arrays of Sets to be passed to it. You probably want this to be to arrays of objects as that is what you are passing in. Also, the way you are declaring it, it should probably be used like:

Set C = Set.fun(A, B)

in which case, fun could be a static function.

there is no reason to declare Set[] C as a public static member in your Set class. In fact this is probably the opposite of what you want.
 
passing arrays in java....hmmm, I forgot.


no wait:

int arary = new int [size]
function(array)

now, the header is:
void function(int anyArray [])
or (int [] anyArray)



 
thx for the fast reply Hector,

the problem (i think) is that i'm not suppose to use static methods, therefore i can't use Set.fun(A, B);

you mentioned that A and B are not instances of Set class, this is where i have a problem

how do i make A and B instances of Set class? and what types should i use to declare A, B, and C in the Set and Test class?

the codes i gave above was just for illustration on where i've come so far, and i'm obviously off track, so i'm asking to make sure i fix the declarations of A, B, and C to get back on track

thx again for the feedback, hope to get more
 
Inside of Tester, you should be declaring C to be an instance of Set. So it should read, Set C = new Set(size).
 
Originally posted by: replicator
shouldn't this be

public Set[] fun(Object[] A, Object[] B) {

i actually screwed up the code above,

but the thing is that i want A, and B to be instances of the Set class, in order for it to turn out like A.fun(B);

i dunno if this is an important info, but i'll type it out anyways in case

the instructions also says you're suppose to write 2 constructors for the Set class

The default constructor Set() is to return an empty set capable of holding 10 elements. Set(n) returns an empty set capable of holding n elements (i assume Set(n) is the 2nd constructor, does this have anything to do with arrays A and B? assuming A, and B as the code in my earlier posts are initiated in class Tester)
 
I dunno but it sounds like u want something like this

public class Tester {
public static void main(String[] args) {
Object[] A = {a, b, c};
Object[] B = {a, b, c, d, e};
...
Set C = new Set(size);

C.fun(A, B);
...
}

public class Set{

private Object[] C;

// default constructor
public Set() {
this.C = new Object[10];
}

public Set(int n) {
this.C = new Object[n];
}

public void fun(Object[] A, Object[] B) {
...
}

edit: do u really need to return anything in fun? You don't seem to in the example that u posted.
 
Meh - I don't know. I dropped out of Java because it was about as interesting as mold. IN fact, mold might even be more interesting.
 
thx replicator, i did figure it out later that day, and it was pretty much what you wrote.

only i made A and B Set Objects, and then i passed them to the Set class' fun method
 
Back
Top