There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
audio avg avg 8 blue screen brand new codec control panel conversion crash delete personal data desktop display dos driver duplicate dvd error error message excel explorer file firefox game graphics hardware hijackthis log install installation internet itunes javascript laptop macro malware monitor msconfig msn music network outlook outlook 2003 outlook express php problem program random rundll32 security seo sound sp3 spyware switch tag cloud trojan usb video virtumonde virus vista visual basic vundo wallpaper windows windows vista windows xp wireless word xp sp3 youtube
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
JAVA Theory


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Geek4Life's Avatar
Member with 63 posts.
 
Join Date: Jan 2008
Experience: Intermediate
07-May-2008, 08:46 PM #1
JAVA Theory
Without giving specifics i want the general on this: is it possible to pass an entire array between two classes (i.e. assuming we have Class A:
Code:
Pubcodelic Class A {
...
int x[][] = new int[#][#]
c b = new b();
...
c.doWhatever()
}
and then another class, b:
Code:
Public Class b {
...
public void doWhatever()
...
}
so, then how can i pass all the values in the array x, in class A, to class B?

Can it be done....semi-easily?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,213 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.58" E
Experience: Intermediate
08-May-2008, 02:09 AM #2
Hi Geek4Life,

Yes, it can be easily done, either by the constructor or by a setter.
Example :
Code:

public class Test1 {
   private int[][] array;

   public Test1(int[][] array) {
      this.array = array;
   }

   ...
}

public class Test2 {
   private int[][] array;

   public Test2() {
   }

   public void setArray(int[][] array) {
      this.array = array;
   }
}

public class Demo {

   public static void main(String[] args) {
     int[][] a = new int[1][1];
     a[0][0] = 1;
     Test1 t1 = new Test1(a);
     Test2 t2 = new Test2();
     t2.setArray(a);	
  }
}
In my example, all the classes Demo, Test1 and Test2 will work with the same object stored in memory.
If you mean to work with a replication of your array in order to preserve its initial values, then :
Code:

public class Demo {

   public static void main(String[] args) {
     int[][] a = new int[1][1];
     a[0][0] = 1;
     Test1 t1 = new Test1(a.clone());
     Test2 t2 = new Test2();
     t2.setArray(a.clone());	
  }
}
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !
Geek4Life's Avatar
Member with 63 posts.
 
Join Date: Jan 2008
Experience: Intermediate
08-May-2008, 09:43 PM #3
Thank you, I knew i could get an answer here. ill try working with that.
Geek4Life's Avatar
Member with 63 posts.
 
Join Date: Jan 2008
Experience: Intermediate
11-May-2008, 01:04 PM #4
just 2 quick problems with your explanation:

1) Remember I'm an Amature at JAVA - I need to understand step by step how that code works/what it does

2) How do I take the modifications made in class B, back to class A?

Please explain the statements used - I have a Java book that teaches JAVA but it hardly scratches the surface of the surface of the capabilities of JAVA - if im going to learn the more advanced capabilities of JAVA I'm going to need help.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,213 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.58" E
Experience: Intermediate
12-May-2008, 04:56 AM #5
Here we go :

1)
Code:

public class Test1 {
   //an attribute
   private int[][] array;

   //The contructor is expecting an array
   public Test1(int[][] array) {
      /*this.array refers to the only attribute of the Test1 class
      and array refers to the passed parameter to the contructor
      the arrays must have the same type*/
      this.array = array;
   }

   ...
}

public class Test2 {
   //an attribute
   private int[][] array;

   /*The default constructor :
   when no constructor is present,
   the class will get the default one at compilation time*/
   public Test2() {
   }

   /*a method :
   it is called a mutator as it modifies an attribute of the class*/
   public void setArray(int[][] array) {
      /*this.array refers to the only attribute of the Test2 class
      and array refers to the passed parameter to the method
      the arrays must have the same type*/  
      this.array = array;
   }
}

public class Demo {

   //the standard method that launches a Java program
   public static void main(String[] args) {
      /*build a 2 dimensional array of integers with 1 * 1 size :
      as an array is an extension of the Object class,
      a will inherit its methods : clone(), toString(), and so on ...
      System.out.println(a.toString()) will display the hex value of the array pointer*/
      int[][] a = new int[1][1];
      //initialize the only item of the array to 1
      a[0][0] = 1;
      /*Create an instance of the Test1 class passing the pointer of a as parameter
      the coding included inside the constructor of Test1 will be executed
      You may display the value of the pointer generated using System.out.println(t1.toString())
      as Test1 is also an extension of the Object class*/
      Test1 t1 = new Test1(a);
      //Create an instance of the Test2 class
      Test2 t2 = new Test2();
      //Pass a copy of the a array to the method setArray
      t2.setArray(a.clone());
  }
}
2) Simply by adding a method called an accessor.
In my class Test2, I will add an accessor this way :
Code:

public class Test2 {
   private int[][] array;

   public Test2() {
   }

   public void setArray(int[][] array) {
      this.array = array;
   }
   
   //Accessor
   public int[][] getArray() {
      return this.array;
   }   
}
In my Demo class, I may have for example :
Code:

      int[][] a = new int[1][1];
      a[0][0] = 1;
      Test2 t2 = new Test2();
      t2.setArray(a.clone());
      int[][] b = t2.getArray();
tomdkat's Avatar
Computer Specs
Distinguished Member with 2,155 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
12-May-2008, 01:01 PM #6
Quote:
Originally Posted by Chicon View Post
Yes, it can be easily done, either by the constructor or by a setter.
Why must the array be a member of the target class for it to operate on it?

Code:
Public Class Test2 {
...
public void doWhatever(int x[][])
...
}

Public Class Test1 {
...
int x[][] = new int[#][#]
Test2 b = new Test2();
...
b.doWhatever(x)
}
Peace...
Chicon's Avatar
Computer Specs
Distinguished Member with 6,213 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.58" E
Experience: Intermediate
12-May-2008, 01:34 PM #7
Quote:
Originally Posted by tomdkat View Post
Why must the array be a member of the target class for it to operate on it? ...
Hi tomdkat,

It is not mandatory to have the array as a member of a class.
My example is 'biased' toward plain old Java objects for didactic reasons.
tomdkat's Avatar
Computer Specs
Distinguished Member with 2,155 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
12-May-2008, 01:59 PM #8
Ok.

Peace...
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:02 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.