Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 23rd, 2007, 07:28 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default array of arrays-objects

Hi everyone. I hope someone can help me. Id like a guidance not solution to my code. I am having trouble understanding objects in array. My program looks something like this. It a sort of the form that user have options typing in number or name. The form had like 9 fields and I want to be able to create array of those 9 fields that would be able to accept and recognize when user inputs name or number.Would it be possible to have like one class create array and somehow get objects(name or number) from another class where that other class would know about that object(name or number). Is this actually possible to do and how.I have tried so many websites and its really hard to understand. Can object know about name and number and if so how do i do that and how do one class gets that info from another class. I am thankfull for any help at all :)

Reply With Quote
  #2 (permalink)  
Old February 11th, 2007, 08:12 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Yeh, you can have an array of Objects. And you can have one object act as a container for a collection of other objects. Here's a toy example (I normally find things easier to follow in code):

Code:
// A very simple object, but it could be a gui element,
// a complex lump of data or any other kind of object
class Example {
  private String name;

  // java lets you get away without setting a constructor
  // google java default constructor for details

  public void setName(String n) {
    name = n;
  }

  public String getName() {
    return name;
  }
}

// Example Array is a 'collection' of Example objects
public class ExampleArray {
  private Example[] egArray; // the container for the collection
  private int size;          // the size of the collection

  // Here's our constructor
  public ExampleArray(int s) {
    size = s;
    // Set up our array
    egArray = new Example[size];

    // Fill it with Example objects
    for (int i=0;i<size;i++) {
      egArray[i] = new Example();
      egArray[i].setName("name: " + i);
    }
  }

  // A little convienience method - loop
  // through our collection and call the getName()
  // method on each object in it
  public void print() {
    for (int i=0;i<size;i++) {
      System.out.println(egArray[i].getName());
    }
  }

  // Right, lets check that works
  public static void main (String argv[]) {
    System.out.println("Object Array Example\n===================");
    // Create a new ExampleArray, with nine elements
    ExampleArray e = new ExampleArray(9);
    // And print it out
    e.print();
  }
}
And here's the output:

Code:
charlie@mogadon:~$ javac ExampleArray.java
charlie@mogadon:~$ java ExampleArray
Object Array Example
===================
name: 0
name: 1
name: 2
name: 3
name: 4
name: 5
name: 6
name: 7
name: 8
charlie@mogadon:~$
Hopefully that should make sense ...

Cheers,
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How access fields of objects stored in Array List rfinks VB How-To 2 July 11th, 2006 06:00 PM
Assoc. Array of Objects hmmm PHP How-To 1 June 19th, 2006 11:38 AM
associative arrays as objects left out adspring BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 June 5th, 2006 01:37 PM
passing jagged array of objects nutrino Beginning VB 6 0 January 27th, 2006 10:58 AM
Dynamically Creating Objects of Array of Classes dinkarsinha General .NET 0 January 28th, 2005 05:22 AM



All times are GMT -4. The time now is 05:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.