How Can I make 2D array with different data type? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How Can I make 2D array with different data type?

I know it's possible if the array type was class/object data type, but how can I store the values I want in a specific position? I want some elements to be Integers, some String, and some character. please explain to me with a clear and easy example. Thank you so much ❤

14th Feb 2017, 12:50 PM
Sooso Sweet
Sooso Sweet - avatar
19 Answers
+ 2
Your main will look something like this Class Students String firstname int mark getInfo() printInfo() //Main get user input for how many students -> nStudents create an array of your class Students[] one = new Students[nStudents]; you must create an object. one[x] = new Students(); then you can use a method to getInfo for one[x]; For loop to create objects and getInfo for all students x -> nStudents another for loop is to printInfo for all Students in the array. Your output file suggests you need to convert mark into a grade as well. example 80-89 = B. arraylist and iterators not needed. Try to getInfo and printInfo for one object in the array first, then the rest is just loops. Students[] one = new Students[2]; // you can replace 2 with nStudents one[0] = new Students(); one[0].getInfo(); one[0].printInfo();
15th Feb 2017, 7:39 AM
Michael W
Michael W - avatar
+ 1
I don't know of a 2D array that can be of two different data types but what you can try is to declare your 2D array as String because then it can take all possible values but it will be placed in Strings variables. Example: (Java) String[][] str = {{"1", "2", "3"},{"A", "B", "C"}} ;
14th Feb 2017, 1:03 PM
Ghauth Christians
Ghauth Christians - avatar
+ 1
String[ ][ ] personData = new String[2 ][4 ]; personData[0][0] = "First Name"; personData[0][1] = "Last Name"; personData[0][2] = "Mark"; personData[0][3] = "Grade"; personData[1][0] = "Joe"; personData[1][1] = "Schofield"; personData[1][2] = "100"; personData[1][3] = "A"; The above populates the two dimensional array, however if you wish to make a two dimensional array for arbitrary amount of people, you need to think about represent all the data that belongs to a person as a separate entity then it would be easy to iterate over those entities and assign them to the two dimensional array appropriately.
14th Feb 2017, 1:03 PM
Ousmane Diaw
+ 1
Array containing 2 different data types is not possible; how many dimensions of the array is irrelevant. http://stackoverflow.com/questions/2015538/arrays-with-different-datatypes-i-e-strings-and-integers-objectorientend Check best answer on this link green check mark, he explains your solution very well. the answer with 27 points, 2nd one down.
14th Feb 2017, 1:08 PM
Michael W
Michael W - avatar
+ 1
@sooso sweet , put your code on stack overflow question area describing your problem. You'll get immediate help and solutions.
14th Feb 2017, 1:22 PM
Ousmane Diaw
+ 1
It is not possible to have a 1-D array with two different types; nor is it possible with a 2-D or 3-D array. A primitive array and an object array are not the same. The array i created in the code is an array of objects. You can have multiple datatypes; String, double, int, and other object types within a single element of the arrray, ie objArray[0] can contain as many different data types as you need. Using a 2-D array has absolutely no affect on the output, but how the data is allocated. It is like you want to use two different sticks of lipstick, but they are exactly the same color; RED. You just want to put on a layer of red with 2 different sticks, but no mater what the outcome is the same; your lips are red... putting on 2 different layers with the same stick is exactly the same output. Using 2 different sticks of lipstick is confusing, no purpose.
15th Feb 2017, 10:38 PM
Michael W
Michael W - avatar
0
@Michael W the link you have given me is really close to my problem, but I still couldn't understand the solution 😢 can you please help me by giving me your email so I can send you my source file in Java, NetBeans and you fix it? Thank you so much for everyone, I really really appreciate your helps 💕
14th Feb 2017, 1:21 PM
Sooso Sweet
Sooso Sweet - avatar
0
@Ousmane Mahy Diaw well my code is long and full of errors because I worked on it as 1D array and I was working to change it to 2D array, here's my code in the link below: https://i.imgur.com/DbZUPoi.png and here's the run I want to reach: https://m.imgur.com/ejDTebM?r sorry in the picture of run It should be 4 students as the user have entered but I cut the picture just to show an example
14th Feb 2017, 1:38 PM
Sooso Sweet
Sooso Sweet - avatar
0
But do you really have to use a two dimensional array????. The picture you've provided can easily be done with toString() methods.
14th Feb 2017, 2:06 PM
Ousmane Diaw
0
yes because I want to keep the Marks as Integers and the Grade as character 🙁
14th Feb 2017, 2:12 PM
Sooso Sweet
Sooso Sweet - avatar
0
yes I want to do it by 2D array, I could do it easily by 1D array in the link below: https://i.imgur.com/nQSjNLI.png but the run I'm trying to reach seemed like a perfect matrix not like mine, more the name is longer more the spaces will be imperfect, so I thought it should be done by 2D array and I also want to keep Marks as Integer and Grade as character
14th Feb 2017, 2:24 PM
Sooso Sweet
Sooso Sweet - avatar
0
Simple design for you Student class { declare 4 members ie first name, last, mark, grade make 4 methods, getFirstname, getLast, getMark, getGrade } Main{ GetHowMany students ie. scanner numberOfStudents = new Scanner (System.in); Make array of students [numberOfStudents] Loop through arrays and all their get methods. output ALL your data } end of program.
14th Feb 2017, 2:24 PM
Michael W
Michael W - avatar
0
@Michael W I will try do that now, hopefully I won't face a problem and be able to do it without using manually ways in assigning values
14th Feb 2017, 2:30 PM
Sooso Sweet
Sooso Sweet - avatar
0
@Michael W can you please explain this step " Loop through arrays and all their get methods. " it's my only problem for now
14th Feb 2017, 3:12 PM
Sooso Sweet
Sooso Sweet - avatar
0
The link i sent to stackoverflow with the solution very close to what you need is there. Study/review "ArrayLists", then study "iterators". I would gladly help you more later, however I must go to work now.
14th Feb 2017, 3:32 PM
Michael W
Michael W - avatar
0
alright, Thank you so muuuch 💖
14th Feb 2017, 3:34 PM
Sooso Sweet
Sooso Sweet - avatar
0
I'm still looking to learn the answer 😢 to be honest I didn't study/review Arrays lists and iterators 🤕
15th Feb 2017, 3:45 AM
Sooso Sweet
Sooso Sweet - avatar
0
your answer is by one dimensional array, I could solve it easily by this way I just don't know how can it be by two dimensional array, I remember I tried to do it last night and it didn't work with me, I really wonder how can I solve it by two dimensional array just. and sorry I just could see your answer now Thank You so much Michael W, I really appreciate all your helps ☺
15th Feb 2017, 3:19 PM
Sooso Sweet
Sooso Sweet - avatar
- 1
for example, How to create this by 2 dimensional array: First name Last name Mark Grade Name Name 100 A
14th Feb 2017, 12:54 PM
Sooso Sweet
Sooso Sweet - avatar