Question

a) Explain how can an array store variables of different types. (b) Include a code example....

a) Explain how can an array store variables of different types. (b) Include a code example.

Please don't copy and paste another answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Normal array store variables of same type variables.
  • Using Objects we can create the arrays.
  • This object arrays can hold different type variables.
  • Simply object arrays are hetrogeneous collection of data variables.

Syntax:

Object[] arrayName = new Object[20];

Example:

import java.util.Scanner;

class Main {

public static void main(String[] args) {

Object[] array = new Object[20];

String str1 = new String("Hii Your score is "); // String data type

array[0] = str1; // Asign string to the array

double marks=92.86;

array[1] = marks; // Asign double type to the array

String str2 = new String(" Out of "); // String data type

array[2] = str2; // Asign string to the array

int maxMarks=100;

array[3] = maxMarks; // Asign int type to the array

for(int i=0;i<4;i++) // Loop rund for 4 times

{

System.out.print(array[i]); // print array which contain variables of different types

}

System.out.println();

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
a) Explain how can an array store variables of different types. (b) Include a code example....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT