Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 23-03-2010
Member
 
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]);
	}
}
Reply With Quote
  #3  
Old 23-03-2010
Member
 
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.
Reply With Quote
  #4  
Old 23-03-2010
Member
 
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]);
	}
}
Reply With Quote
  #5  
Old 23-03-2010
Member
 
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.
Reply With Quote
  #6  
Old 23-03-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Array of char and int"
Thread Thread Starter Forum Replies Last Post
Problem in representing 3 x 3 array of char in c++ KAILEY Software Development 5 20-02-2010 08:05 PM
Comparing char array in c++ MAHAH Software Development 5 15-02-2010 08:19 PM
Randomaccessfile using char array TechGate Software Development 5 27-01-2010 11:19 AM
Size of a pointer to an array of char Zool Software Development 3 14-10-2009 12:05 PM
How do i clear char array in c++ B_Hodge Software Development 3 16-05-2009 09:35 AM


All times are GMT +5.5. The time now is 12:32 AM.