The Artima Developer Community
Sponsored Link

Java Answers Forum
classes and arrays

1 reply on 1 page. Most recent reply: Nov 21, 2002 8:41 PM by Daniel Ray

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Adam

Posts: 1
Nickname: adam20000
Registered: Nov, 2002

classes and arrays Posted: Nov 21, 2002 6:48 AM
Reply to this message Reply
Advertisement
I wanna make a 2-dimensional array of a class... e.g. I got:

public class name {
...
}

and then I wanna do something like

name[][] names = new name[1][1];
but it won?t work..I can do it with int and char and that kinda datatypes but now with a own class..


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: classes and arrays Posted: Nov 21, 2002 8:41 PM
Reply to this message Reply
Try it this way. Two public classes here .. so choose how you want to handle that .. make one an inner class or just separate them in different files.

public class Name {
private int id;

public Name() {

}// End Name()

public void setId(int id){
this.id = id;

}// End setId(int)

public int getId(){
return id;

}// End getId()

}// End class Name

public class TestName {
private Name[][] myName;

public TestName() {
myName = new Name[5][5];

}// End TestName()

public void populateMyName(){
int count = 0;

for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
Name newName = new Name();
newName.setId(++count);
myName[j] = newName;

}

}

}// End populateMyName()

public void printMyNameIds(){
Name tempName;

for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
tempName = myName[j];
System.out.println("ID = " + tempName.getId());

}

}

}// End printMyNameIds()

public static void main(String[] args) {
TestName testName = new TestName();

testName.populateMyName();
testName.printMyNameIds();

}// End main(String[])

}// End class TestName

Ray

Flat View: This topic has 1 reply on 1 page
Topic: inserting a csv file into a database Previous Topic   Next Topic Topic: Fat Client

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use