Results 1 to 9 of 9
  1. #1
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Array of custom type

    I need to be able to store the values of these variables in one new array element (located in another different), which im planning to repeat for as long as the user requires. From what I understand you can create an array of custom type which does exactly that. Is that correct? (For example, an array of type RecordTV?)

    Java Code:
            
    public class RecordTV {
    	float serial;
    	float size;
    	float coordinates;
    	String brand;
    	
    	protected RecordTV (float tvserial, float tvsize, 
    			float tvcoordinates, String tvbrand) {
    		
    		settvserial(serial);
    		settvsize(size);
    		settvcoordinates(coordinates);
    		settvbrand(brand);
    	}
    	public void settvserial(float serial) {
    		this.serial = serial;
    	}
    	public void settvsize(float size) {
    		this.size = size;
    	}
    	public void settvcoordinates(float coordinates) {
    		this.coordinate = coordinates;
    	}
    	public void settvbrand(String brand) {
    		this.brand = brand;
    	}
    	
    
    	}

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Yes that is definitely true :) in another class you form the array of Record Tv. but you have some faults in your code.

    1 )
    protected RecordTV (float tvserial, float tvsize,
    float tvcoordinates, String tvbrand) {
    you have to set the float serial = tvserial and not what you have done, this has to be for all your parameters

    since this
    settvserial(serial);
    is translated to mean that set your float serial (that is an instance = 0) as to be itself.
    but you need to set the parameter tvserial to be the serial.
    Did I made my point clear ?
    If that is not clear tell me to make it clearer and may be give a slight example for that :)
    Last edited by baf06; 04-17-2011 at 03:42 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    no I understand, thanks! ok i've tried setting up that array, and im not sure of the correct way to refer to the RecordTV class and its variables.

    Java Code:
    RecordTV [] tvdata = new tvdata [100];
    
    public static void storeTV(RecordTV [] tvdata) {
    		tvdata[0].serial = this.serial;
    {
    However the serial from "this.serial" is in that RecordTV class. is there any way to refer to it without (RecordTV.serial)?

  4. #4
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    getSerial() must do the job, you did some setter in your RecordTV class and you can do some getters that return what you need. for example in getSerial() you will return this.serial; this is the best solution I believe.
    And you referred to the RecorTV correctly, but I believe that it is better not to have this big array when you don't actually know how many input the user will do (this is what you want) so if you know how to work with ArrayList it is better for you code, and if note you can code a method that increase the size of the array when needed, since if you want to read your RecordTV array of 100 elements you will face null exceptions and some errors if not all the elements are filled
    Last edited by baf06; 04-17-2011 at 04:24 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  5. #5
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    thanks again mate, I read about ArrayLists earlier and ill implement that when I get time. Made that get method like you said, but it wants me to refer it with "RecordTV.getSerial", is there any way to avoid this?

  6. #6
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by whytefish View Post
    thanks again mate, I read about ArrayLists earlier and ill implement that when I get time. Made that get method like you said, but it wants me to refer it with "RecordTV.getSerial", is there any way to avoid this?
    you tried tvdata[0].getSerial() and it didn't work ? It should work, I have just a similar code working properly that way.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  7. #7
    whytefish is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    ...no wait, its good haha. thanks for your help mate!

  8. #8
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    lol your are welcome :) glad I helped
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Judging by the names of your variables, I have to question whether float is the right type. Floating point operations are slower than integer operations and sometimes throw out unexpected results (for example, a comparison using == might return false because there's a difference of 0.0000001). Also, for the few occasions where floating point variables are appropriate, you're almost always better off with a double.

    Serial number, for example, is unlikely to have a fractional component, and coordinates looks like it should either be two separate variables or a two-element array. Even with size, which could take a fractional value (for example, 62.5 if it represents centimetres), would be better off as an int with a smaller unit (625 millimetres).

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2011, 05:15 AM
  2. JList with custom Array
    By sony144 in forum Advanced Java
    Replies: 1
    Last Post: 10-09-2010, 06:35 PM
  3. Custom class in an array?
    By bugmenot in forum New To Java
    Replies: 5
    Last Post: 04-15-2009, 12:10 AM
  4. Custom XML to Java Type Binding using JAXB
    By surajkumar in forum XML
    Replies: 0
    Last Post: 09-02-2008, 12:32 PM
  5. type casting array?
    By willemjav in forum Advanced Java
    Replies: 5
    Last Post: 08-27-2008, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •