Please I need a small help concerning a code java, whenever I came to initialise a table as a attribute in a class I didn't find the definition logic, since no [] setted there so I'm kind of curious to know with an example of class which work with a table attribute & should I initialise the table in the default constructor by null? So, I did my best but I can't understand this code I had wrote it & of course there will be many errors as I think:
public class ClasseSMI {
private String _filiereName;
private String[] _etudiantsList;
public ClasseSMI()
{
this._filiereName ="jjjjj";
this._etudiantsList = null;
}
public String toString() {
return _filiereName + " " + _etudiantsList;
}
public static void main(String[] args) {
ClasseSMI smi = new ClasseSMI();
System.out.println(smi);
}
}
so any one can help with example please ?
Thanks in advance !
You just began to learn Java. There are so many way to do what you want to achieve. Null initialization is only one way to initialize reference to array. Default but not best one. Here is what you, probably, want:
public class ClasseSMI {
private String _filiereName;
private String[] _etudiantsList;
public ClasseSMI()
{
this._filiereName ="Alex";
this._etudiantsList = new String[]{"Nick","Mark","Nickole"};
}
public String toString() {
String result=_filiereName+":";
for(String etudiant:_etudiantsList){
result+= " "+ etudiant;
}
return result;
//return _filiereName + " " + _etudiantsList;
}
public static void main(String[] args) {
ClasseSMI smi = new ClasseSMI();
System.out.println(smi);
}
}
It returns
Alex: Nick Mark Nickole
If you use your return it will use default toString() for array and it will look like this:
Alex [Ljava.lang.String;@17dfafd1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With