Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2007, 08:28 PM
Registered User
 
Join Date: Jan 2007
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 :)

 
Old February 11th, 2007, 09:12 PM
Friend of Wrox
 
Join Date: Dec 2003
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





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 11:58 AM
Dynamically Creating Objects of Array of Classes dinkarsinha General .NET 0 January 28th, 2005 06:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.