There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
audio avg avg 8 backup bios boot browser bsod computer cpu crash css desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze game graphics hard drive hardware help please hijackthis hjt install internet internet explorer itunes javascript keyboard lan laptop malware missing monitor msn network networking openoffice outlook outlook 2003 outlook express php popups problem router screen seo slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp wireless word
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: 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
Senior Member with 102 posts.
 
Join Date: Jan 2008
Experience: Intermediate
07-May-2008, 08:46 PM #1
Solved: 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,321 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
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
Senior Member with 102 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
Senior Member with 102 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,321 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
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,746 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,321 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
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,746 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
12-May-2008, 01:59 PM #8
Ok.

Peace...
Geek4Life's Avatar
Senior Member with 102 posts.
 
Join Date: Jan 2008
Experience: Intermediate
19-May-2008, 07:19 PM #9
Wait...can it be done with only 2 classes or do i need a second one. (My intention is to create a sort-of defensive AI for the TicTacToe program i recieved assistance on earlier). I do not want you doing my work for me, which is what you kind of did with TicTacToe (nice as it was of you, it wasn't my code, my style of coding, etc., I just want to understand what i need to do so that i can do it myself), or do i need a third class to pass between the 2.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,321 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
20-May-2008, 01:50 AM #10
Quote:
Originally Posted by Geek4Life
Wait...can it be done with only 2 classes or do i need a second one...
Yes, of course : one class, the starter and intializer of the program and the other one, the AI manager of TicTacToe.
Geek4Life's Avatar
Senior Member with 102 posts.
 
Join Date: Jan 2008
Experience: Intermediate
20-May-2008, 07:44 PM #11
But I need to be able to send the entire array in the starter class to the AI class so the AI class can then Analyze and modify it? How am I supposed to do that....that's all I'm really trying to figure out here.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,321 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
20-May-2008, 09:06 PM #12
You may do it this way :
Code:

public class AI {
    public static void analyze(int[][] array) {
        int[][] a = array;
        // for example
        a[1][1] = 3;
    }
}

public class Starter {
    private static int[][] array = { {0,0,0}, {0,0,0}, {0,0,0} };

    public static void main(String[] args) {
        System.out.println("Before : " + array[1][1]);
        AI.analyze(array);
        System.out.println("After : " + array[1][1]);
    }
}
tomdkat's Avatar
Computer Specs
Distinguished Member with 2,746 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
21-May-2008, 12:53 AM #13
Isn't that basically what I posted above?

Peace...
Chicon's Avatar
Computer Specs
Distinguished Member with 6,321 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
21-May-2008, 02:12 AM #14
Hi tomdkat,

Not exactly the same !
In my last example, there's no instantiation of the AI class as its only method, analyze, is static (all static members of an application are preloaded into memory before the program starts effectively) and public(visible from outside its host class and package).
Therefore, there's no need to create an AI object.
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !
tomdkat's Avatar
Computer Specs
Distinguished Member with 2,746 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
21-May-2008, 10:53 AM #15
Gotcha.

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 06:00 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.