Results 1 to 6 of 6

Thread: array trouble

  1. #1
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default array trouble

    just a simple question. Can an array have elements stored in increments of floats rather than integers. "ex. array[.1],array[.2],array[.3],array[.1]". reason is because im going by degrees and i rather have a finer tolerance than by 1's.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,955
    Rep Power
    8

    Default Re: array trouble

    No. But you can use something like a Map.

    Or use ints like usual but define each unit to be as small as you need (like using 150 cents instead of 1.50 dollars).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: array trouble

    The index have to be int. If you want a finer tolerance than whole degrees, you can always multiply it by ten (or even a hundred) before storing it in an array though (so a degree of 1.5 would be in array[15]).

  4. #4
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: array trouble

    thanks i like your guys answers, but i just realized only about 4 of my 800 elements in the array would be a non integer. that would be a lot of wasted space i created an array like that. ill just do some ratio work to figure out the degrees that arent intergers. thanks for your help. but one quick question, say i preset my array so i would have enough spaces are all the spaces im not using still take up memory?
    Java Code:
    double xxx[] = new double[100000];

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: array trouble

    In an array of primitive types (int, long, float etc), I believe it does, yes. In an array of classes, enough space is assigned to contain pointers, which (I think) means four bytes per pointer.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: array trouble

    Regarding your last question on array size. Use List implementations (e.g. ArrayList). The size is adjusted dynamically to accommodate growth.
    Also, try:
    Java Code:
    double [] vals = new double[2_000_000_000];
    //  need this to get underscores to show -- silly formatter.
    And see what happens.

    Regards,
    Jim
    Last edited by jim829; 07-17-2013 at 09:35 PM. Reason: Array size correction
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Having some array trouble
    By beijct in forum New To Java
    Replies: 6
    Last Post: 11-10-2011, 03:44 AM
  2. Trouble copying an array
    By xXRedneckXx in forum New To Java
    Replies: 10
    Last Post: 02-05-2011, 05:36 PM
  3. Having trouble with 2d Array
    By geekchick in forum New To Java
    Replies: 2
    Last Post: 08-02-2010, 04:54 AM
  4. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 12:04 PM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:06 AM

Tags for this Thread

Posting Permissions

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