Results 1 to 6 of 6

Thread: Array of char and int

  1. #1
    Join Date
    Mar 2010
    Posts
    197

    Array of char and int

    Hello,
    I want to create a matrix containing integers and characters. However in the various examples I have seen tables or matrices presented possess a single type. Is there a solution to my problem? If you guys have a solution for this then please let me know as i am very much interested in this. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Array of char and int

    Hello,
    The following statement works, but I think it involves storing characters as integers:
    Code:
    int[][] mat = {{1, 2, 3},{4, 'a', 6},{'a', 'b', 'c'}};
    It is indeed the case, as shown in the code below:
    Code:
    Public class Test {
    	Public static void hand(String[] args) {
    		int[][] a = {{1, 2, 3},{4, 'a', 6},{'a', 'b', 'c'}}; 
    		
    		for(int x =0; x <a.length; x + +)
    			for(int y =0J <a[x].lengthJ + +)
    				System.out.System.out.println(a[x][y]);
    	}
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Array of char and int

    Hello,
    The above posted is a good solution, another solution is that you can give an object type has your picture, like that you can store what you want in it:
    Here is the part of code for the same
    Code:
    Object[][] mat = {{1, 2, 3}, {'a', 'b', 'c'}};
    If you have been round with java object and the constructors , then you can pretty easily do this and obtain the required results.

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Array of char and int

    Hello,
    Check out the below code, I think this can help you.
    Code:
    Public class Test {
    	Public static void hand(String[] args) {
    		Object[][] a = {{new Integer(1), new Integer(2), new Integer(3)},{new Character('a'), new Character('b'), new Character('c')}}; 
    		
    		for(int i =0; I <a.length; I + +)
    			for(int y =0J <a[i].lengthJ + +)
    				System.out.System.out.println(a[i][y]);
    	}
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Array of char and int

    Hello,
    If I am not wrong since java 5, java manages autoboxing, i.e it automatically converts the type of object bases, so
    Code:
    int a = new Integer (3)
    and
    Code:
    Integer b = 2;
    and you can use java 5 easily and the instruction are also valid. You can download the java from their official site and install it and use the new packages.

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: using char and int in array

    Hello,
    I think the perfect osltuion for your problem will be to cast it to int integer value.
    Code:
    (int)matrix[0][0];
    More better way to do this is
    Code:
    (Integer)matrix[0][0];
    and is normally good way to represent a integer value. If you need any more help regarding this topic then you can post back.
    Last edited by GlassFish; 23-03-2010 at 09:56 AM.

Similar Threads

  1. Problem in representing 3 x 3 array of char in c++
    By KAILEY in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 08:05 PM
  2. Comparing char array in c++
    By MAHAH in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 08:19 PM
  3. Randomaccessfile using char array
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 11:19 AM
  4. Size of a pointer to an array of char
    By Zool in forum Software Development
    Replies: 3
    Last Post: 14-10-2009, 12:05 PM
  5. How do i clear char array in c++
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 16-05-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,118,381.91721 seconds with 17 queries