Results 1 to 19 of 19
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2

Thread: Creating an array with different data types

  1. #1
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default Creating an array with different data types

    Hi.
    I want to create an array that I can put Integers in some of it's elements, and Strings in other elements. Is that possible?
    Thanks in advance

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Investigate

    Java Code:
    Object[] arr;
    But you might get other, more useful, suggestions if you describe what you are trying to do rather than how you are trying to do it. An array of objects is a somewhat vague type of thing.
    Last edited by pbrockway2; 08-09-2011 at 02:05 PM.

  3. #3
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Well, I have to relate the size of a file and it's last access to it's type. So i thought if I did a 2D array that would have the size in one colomn, the last access in another and the type in the 3rd colomn..
    The Object[] arr; you said, can you be more specific?
    Thanks

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    More specifically, try declaring an object array of length 3 and then assigning an integral number, a date and something representing a filetype to the three elements.

    -----

    The better suggestion would be to define a class with the three quantities as instance variables. The data as a whole would then be modelled as a one dimensional array of instances of this class. The advantage of this approach is that you would obtain the access time for example from a "getter" which returns a specific type - a Date - and the compiler would catch mistakes like using the wrong index and thinking you were working with the file size or whatever.
    JeffGrigg likes this.

  5. #5
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Rami View Post
    Hi.
    I want to create an array that I can put Integers in some of it's elements, and Strings in other elements. Is that possible?
    Thanks in advance
    You could use a Vector or ArrayList. They will store Objects like an array of Objects except that they can be resized. I find them very useful.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    A List (Vector, ArrayList, etc) which is declared to hold instances of Object suffers from the same problem as an array of Object, namely that the type of the elements is not being specified anywhere. Whether to use a collection rather than an array is a question quite distinct from the one asked in this thread to which the answer remains: declare and use a class to represent file access information.

    See, for instance, this discussion at stackoverflow.com.
    Fubarable likes this.

  7. #7
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    After reading through my text, it sounds like you are looking for an Arraylist. I would read up on those.

    Just remember that Arraylist stores objects. So, if you call the information pointed to by A[i], and it is an integer, you will need to typecast that data before you can use it in an equation. This is because you will be calling out an object from the array, not an integer. thankfully, one line of code will typecast it for you. Same deal if the data you are recalling from that index is a String, you will need to typecast it as a string before use.
    Last edited by Willriker; 08-10-2011 at 05:59 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Willriker View Post
    After reading through my text, it sounds like you are looking for an Arraylist. I would read up on those.

    Just remember that Arraylist stores objects. So, if you call the information pointed to by A[i], and it is an integer, you will need to typecast that data before you can use it in an equation. This is because you will be calling out an object from the array, not an integer. thankfully, one line of code will typecast it for you. Same deal if the data you are recalling from that index is a String, you will need to typecast it as a string before use.
    What? You can't "typecast" an int into a String as that simply won't compile. And besides if you want to represent int as an object, you'd use an Integer, not a String, and autoboxing should handle this for the coder. And besides, what pbrockway2 has said about not using arrays or array lists of different types still applies.

  9. #9
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    More specifically, try declaring an object array of length 3 and then assigning an integral number, a date and something representing a filetype to the three elements.

    -----

    The better suggestion would be to define a class with the three quantities as instance variables. The data as a whole would then be modelled as a one dimensional array of instances of this class. The advantage of this approach is that you would obtain the access time for example from a "getter" which returns a specific type - a Date - and the compiler would catch mistakes like using the wrong index and thinking you were working with the file size or whatever.

    Thanks! This is helpful! But I didn't understand how this approach would let me obtain the access time for example with returning the specific time? I'm starting to like it from what you are saying!

  10. #10
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What? You can't "typecast" an int into a String as that simply won't compile. And besides if you want to represent int as an object, you'd use an Integer, not a String, and autoboxing should handle this for the coder. And besides, what pbrockway2 has said about not using arrays or array lists of different types still applies.
    I think I agree, because in the array, it would hold elements as integers and strings not as objects of a class. Is this true and precise?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Rami View Post
    I think I agree, because in the array, it would hold elements as integers and strings not as objects of a class. Is this true and precise?
    No, not true: It would hold references to objects which may be Integer, String or other objects, or even no object (null).

  12. #12
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, not true: It would hold references to objects which may be Integer, String or other objects, or even no object (null).
    ok! Now I understand! :)
    Thanks! :)

  13. #13
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, not true: It would hold references to objects which may be Integer, String or other objects, or even no object (null).
    Yeah, this is what i meant to say. If you reference an index that is the object "23", youll need to typecast it to an int. If you reference an index that is the object "name", youll need to type cast it as a string before using it elsewhere.

  14. #14
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Willriker View Post
    Yeah, this is what i meant to say. If you reference an index that is the object "23", youll need to typecast it to an int. If you reference an index that is the object "name", youll need to type cast it as a string before using it elsewhere.
    ok explain this more?! how is that?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Willriker View Post
    Yeah, this is what i meant to say. If you reference an index that is the object "23", youll need to typecast it to an int. If you reference an index that is the object "name", youll need to type cast it as a string before using it elsewhere.
    You can't cast an Integer into an int. It just won't work. You would cast to (Integer), but all this casting has a bad smell to it.

    Original poster: the true answer to this problem as we've been stating all along is not to muck with arrays or ArrayLists that hold different types. Again create a class to describe a "record" of data, and then create an array or ArrayList (or other collection) of objects of this class.

  16. #16
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You can't cast an Integer into an int. It just won't work. You would cast to (Integer), but all this casting has a bad smell to it.

    Original poster: the true answer to this problem as we've been stating all along is not to muck with arrays or ArrayLists that hold different types. Again create a class to describe a "record" of data, and then create an array or ArrayList (or other collection) of objects of this class.
    ok.. I'm working on it.. Ill let u know if I had problems because I'm not so good at java :)
    Thanks alot again! you've been a great help!

  17. #17
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    7.3.3 ArrrayLists
    The DynamicArrayOfInt class could be used in any situation where an array of int with no preset limit on the size is needed. However, if we want to store Shapes instead of ints, we would have to define a new class to do it. That class, probably named "DynamicArrayOfShape", would look exactly the same as the DynamicArrayOfInt class except that everywhere the type "int" appears, it would be replaced by the type "Shape". Similarly, we could define a DynamicArrayOfDouble class, a DynamicArrayOfPlayer class, and so on. But there is something a little silly about this, since all these classes are close to being identical. It would be nice to be able to write some kind of source code, once and for all, that could be used to generate any of these classes on demand, given the type of value that we want to store. This would be an example of generic programming. Some programming languages, including C++, have had support for generic programming for some time. With version 5.0, Java introduced true generic programming, but even before that it had something that was very similar: One can come close to generic programming in Java by working with data structures that contain elements of type Object. We will first consider the almost-generic programming that has been available in Java from the beginning, and then we will look at the change that was introduced in Java 5.0. A full discussion of generic programming will be given in Chapter 10.

    In Java, every class is a subclass of the class named Object. This means that every object can be assigned to a variable of type Object. Any object can be put into an array of type Object[]. If we defined a DynamicArrayOfObject class, then we could store objects of any type. This is not true generic programming, and it doesn't apply to the primitive types such as int and double. But it does come close. In fact, there is no need for us to define a DynamicArrayOfObject class. Java already has a standard class named ArrayList that serves much the same purpose. The ArrayList class is in the package java.util, so if you want to use it in a program, you should put the directive "import java.util.ArrayList;" at the beginning of your source code file.

    The ArrayList class differs from my DynamicArrayOfInt class in that an ArrayList object always has a definite size, and it is illegal to refer to a position in the ArrayList that lies outside its size. In this, an ArrayList is more like a regular array. However, the size of an ArrayList can be increased at will.


    ... skipped some text...

    All this works very nicely. The only slight difficulty arises when you use the function players.get(i) to get the value stored at position i in the ArrayList. The return type of this function is Object. In this case the object that is returned by the function is actually of type Player. In order to do anything useful with the returned value, it's usually necessary to type-cast it to type Player:

    Player plr = (Player)players.get(i);
    For example, if the Player class includes an instance method makeMove() that is called to allow a player to make a move in the game, then the code for letting every player make a move is

    for (int i = 0; i < players.size(); i++) {
    Player plr = (Player)players.get(i);
    plr.makeMove();
    }
    The two lines inside the for loop can be combined into a single line:

    ((Player)players.get(i)).makeMove();

    This gets an item from the list, type-casts it, and then calls the makeMove() method on the resulting Player. The parentheses around "(Player)players.get(i)" are required because of Java's precedence rules. The parentheses force the type-cast to be performed before the makeMove() method is called.
    Javanotes 6.0, Section 7.3 -- Dynamic Arrays and ArrayLists

    I am very sorry if i am sounding like a bit of a pest, but i had intentions of producing a program using an array list so that i can work with items of multiple types. From what you are saying i might have to rethink my logic, my plan of action for the program.

    I took this to mean that the statements players.get(i) and players.get(i+1) would both return objects. index[i] could refer to an object whose value would be, for example, "First Batter". To use index[i] elsewhere, you would have to type cast it into a string first, because it is currently an object. index[i+1] would reffer to an object whose value is, "32". to use index[i+1] elewhere, you would need to typecast it to int or double.

    Am i correct in this interpretation, or am i just way off...
    Last edited by Willriker; 08-10-2011 at 06:52 PM.

  18. #18
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Pbrockway has given you the best advice in this thread. I'm surprised more people haven't told you not to do what you're asking how to do.
    Get in the habit of using standard Java naming conventions!

  19. #19
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    It is my understanding that casting is usually reserved for primitive types to convert to other primitive types:
    double x;
    int j = 12;
    x = (double)j ;

    If you want to convert and int(primitive) to a String(class) the Integer class has a static method:

    Integer.toString( int i )

    Conversely if you want to convert a String to an int then use the following:

    Integer.parseInt( String s )
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. Java Data Types ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 05:43 PM
  2. Creating an array from data in a text file
    By Ryanas in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 03:46 PM
  3. Comparing two data types in order to store in array
    By gwithey in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 11:27 AM
  4. Replies: 11
    Last Post: 04-01-2009, 04:43 AM
  5. Array of different data types?
    By venkatteshb in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 06:42 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
  •