473,378 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

2D in Java

nanhiPari
hello..

making a Project..n want to ask the Guru's of Java..

can java provide me a method so that i can store multiple phone no's in one variable n this phone nos corresponds to a Address..

well what i thought was a 2D Array for this..but when i made it, the data was not stored in the variable coz i have not given the SIze(coz it can Varry) of the Array..

Any solution...??

Thanz in Advance

Cheerz!!
Jul 15 '07 #1
6 1279
JosAH
11,448 Expert 8TB
hello..

making a Project..n want to ask the Guru's of Java..

can java provide me a method so that i can store multiple phone no's in one variable n this phone nos corresponds to a Address..

well what i thought was a 2D Array for this..but when i made it, the data was not stored in the variable coz i have not given the SIze(coz it can Varry) of the Array..

Any solution...??

Thanz in Advance

Cheerz!!
Basically an Address can have zero or more PhoneNumbers? Why not just do this:

Expand|Select|Wrap|Line Numbers
  1. public class PhoneNumber {
  2.    // all the obligatory methods in here
  3. }
  4. //
  5. public class Address {
  6.    private Set<PhoneNumber> phones= new HashSet<PhoneNumber>();
  7.    // ...
  8.    public boolean addPhoneNumber(PhoneNumber pn) {
  9.       return phones.add(pn);
  10.    }
  11.    public boolean hasPhoneNumber(PhoneNumber pn) {
  12.       return phones.contains(pn);
  13.    }
  14.    // etc.
  15. }
  16.  
kind regards,

Jos
Jul 15 '07 #2
Basically an Address can have zero or more PhoneNumbers? Why not just do this:

Expand|Select|Wrap|Line Numbers
  1. public class PhoneNumber {
  2.    // all the obligatory methods in here
  3. }
  4. //
  5. public class Address {
  6.    private Set<PhoneNumber> phones= new HashSet<PhoneNumber>();
  7.    // ...
  8.    public boolean addPhoneNumber(PhoneNumber pn) {
  9.       return phones.add(pn);
  10.    }
  11.    public boolean hasPhoneNumber(PhoneNumber pn) {
  12.       return phones.contains(pn);
  13.    }
  14.    // etc.
  15. }
  16.  
kind regards,

Jos
Thankz for the suggestion Jos..but where i m doing this Project(my college..) it has java 1.4 n Generic is not Supported on that version..

well any other way to do this Thing..??

Thankz

Cheerz!!
Jul 15 '07 #3
JosAH
11,448 Expert 8TB
Thankz for the suggestion Jos..but where i m doing this Project(my college..) it has java 1.4 n Generic is not Supported on that version..

well any other way to do this Thing..??

Thankz

Cheerz!!
Sure, do it the old-fashioned way with Lists and casts and all. There's no real
difference except for the verbosity in your code, i.e. the general idea doesn't change.

kind regards,

Jos
Jul 15 '07 #4
r035198x
13,262 8TB
.. and don't forget there is a full page on HashSet in the Java API specs.
Jul 15 '07 #5
Sure, do it the old-fashioned way with Lists and casts and all. There's no real
difference except for the verbosity in your code, i.e. the general idea doesn't change.

kind regards,

Jos
.. and don't forget there is a full page on HashSet in the Java API specs.

Thanz for the info..

thanz Jos..will try it that way..n see..whether it works or not in my case...

cheerz!!
Jul 15 '07 #6
JosAH
11,448 Expert 8TB
Thanz for the info..

thanz Jos..will try it that way..n see..whether it works or not in my case...

cheerz!!
Of course it will: suppose you have 'T' type of elements in your list; the Java 1.5
way would be something like this (using the new for loop):

Expand|Select|Wrap|Line Numbers
  1. for (T t: list)
  2.    // do something with that 't'
  3.  
In 'old' Java 1.4 you'd have to do this:

Expand|Select|Wrap|Line Numbers
  1. for (int i= 0, n= list.size(); i < n; i++) {
  2.    T t= (T)list.get(i);
  3.    // do something with that 't'
  4. }
  5.  
kind regards,

Jos
Jul 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.