Results 1 to 6 of 6
  1. #1
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default How can I do this? Combine variable.

    int Mem1, mem2, mem3, mem4, mem5;
    int a1=1, a2=2, a3=3, a4=4, a5=5;
    i is 2


    can I do:
    mem[i]=a[i]



    so the result is:
    mem2=2



    Can this be done? Or is there another way around?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    That is correct. It is the TYPE of the elements of the array that matter if you can assign one to the other.

    If type in array is int and in another array is int then a[i]=b[i];works.

    Same thing for any Object, but not collections of objects.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Can I make the array start from 1 instead of 0? how do i do that?

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    If you're trying to do what I think you're trying to do, NO, you can't.
    Java Code:
    mem[i] = a[i] //this assigns a value from the "a" array to the "mem" array. It does not put an index on the "mem" variable name.
    You might to investigate using maps (hashmaps) for assiging a values to keys.

    As for your question about the arrays starting from 1, no, all arrays indexes start at 0. You can change your program so that it thinks it is starting with 1, but array's index always starts with 0.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    Yep, study Collections.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Peter, I understand what you want to do, since I was able to do that in a legacy language I used a lot. No, there is no way to create data structures in Java to overlay a bunch of fields with an array. You either create individual variables or use arrays. The approach you use depends on your situation. Sorry...

Similar Threads

  1. [SOLVED] How do I combine two variables?
    By bobleny in forum New To Java
    Replies: 12
    Last Post: 07-08-2008, 03:15 PM
  2. How to combine mysql and java?
    By sandeeprao.techno in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 04:41 AM
  3. Combine package of(jdk,eclipse and tomcat)
    By joseph in forum Eclipse
    Replies: 0
    Last Post: 04-07-2008, 01:18 PM
  4. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 01:42 AM
  5. Getting variable value from a variable name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:26 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
  •