d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java & Arraylist
Add Reply New Topic New Poll
Member
Posts: 361
Joined: Apr 13 2008
Gold: 0.40
Dec 2 2012 06:01pm
I want to pass values from subclass ArrayList to superclass
Code

public class ABC { // The superclass
   static ArrayList<CINEMA> xxx = new ArrayList<CINEMA>();

// more code, including main func


Code

// class constructor
       public CINEMA(Integer id, String title, Double price, String hour, String desc)
       {
           super();
           setID(id);
           setTitle(title);
           setPrice(price);
           setHour(hour);
           setDesc(desc);
       }


Code

// Subclass
public class WORKER extends ABC {

   public void AddSession(Integer id, String title, Double price, String hour, String desc)
   {
       CINEMA tmp = new CINEMA(2332, "xaxa", 23.30, "10:20", "");
       super.xxx.add(tmp);
   }
}


output:
cinema.XXX@8814e9

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 2 2012 06:11pm
what is your question?

I have two guesses at what you want:
1) override CINEMA's toString() method to show a more appropriate string when calling System.out.println(xxx)
2) remove static from xxx's declaration
Member
Posts: 361
Joined: Apr 13 2008
Gold: 0.40
Dec 2 2012 06:26pm
I cant remove static from xxx declaration, because i wont be able to print it in static void Main

if i remove static from main, Netbeans will not find main function

and the question is, how to print correct values. At now it looks like a pointer

Here is example of working array

Code
public class Testit {
   protected static ArrayList<Integer> aint = new ArrayList<Integer>();
   
   public static void main(String[] args) {
       subclass sub = new subclass();
       
       sub.doit();
       
       System.out.println(aint.get(0));
       
   }
}


Code
public class subclass extends Testit
{
   public void doit()
   {
       super.aint.add(5);
   }
}


but my wont work, i think is constructor fault :(

The main difference is that, that there is ArrayList<Integer>, My class is constructed from int, string, and double values, so it can be the reason

This post was edited by Sobiech12118 on Dec 2 2012 06:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 2 2012 06:41pm
Quote (Sobiech12118 @ Dec 2 2012 07:26pm)

and the question is, how to print correct values. At now it looks like a pointer

Refer to #1 in my post above.

Member
Posts: 361
Joined: Apr 13 2008
Gold: 0.40
Dec 2 2012 07:00pm
System.out.println(xxx.get(0).toString());

nope, the result is the same ;\

or I missunderstand You. I am newbie with Java :cry:

This post was edited by Sobiech12118 on Dec 2 2012 07:00pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 2 2012 07:10pm
Quote (Sobiech12118 @ Dec 2 2012 08:00pm)
System.out.println(xxx.get(0).toString());

nope, the result is the same ;\

or I missunderstand You. I am newbie with Java  :cry:


thats what i'm telling you. it's invoking the toString() method to try and print it. if you want a more meaningful representation, override it.

public String toString(){
return "blah blah blah";
}
Member
Posts: 361
Joined: Apr 13 2008
Gold: 0.40
Dec 2 2012 07:25pm
I still dont get it, but I changed all vars to String type (with same result)

What do you mean by override it?

This post was edited by Sobiech12118 on Dec 2 2012 07:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 2 2012 08:07pm
Quote (Sobiech12118 @ Dec 2 2012 08:25pm)
I still dont get it, but I changed all vars to String type (with same result)

What do you mean by override it?


override toString() is the clarification. just google "java tostring" if you're still confused.
Member
Posts: 361
Joined: Apr 13 2008
Gold: 0.40
Dec 2 2012 08:18pm
Oh :D

Now i get it ;)

I ve found this article (by googling "java tostring" :D)
http://www.javaservletsjspweb.in/2012/08/java-tostring-method-customizing-it-for.html#.ULwK-4acQZ4

Thanks for patience :) It will be enought for me :)

Edit: Done :) Its working like a charm :)

This post was edited by Sobiech12118 on Dec 2 2012 08:23pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll