public class overLoadEg {
//array that will store integers
static int[] intArray = new int[10];
//array that will store doubles
static double[] doubleArray = new double[10];
//array that will store strings
static String[] stringArray = new String[10];
static int i = 0, j = 0, k = 0, m, n;
public static void main(String[] args) {
//input values
data(23);
data(23.4554);
data("Hello");
data("world");
data("help");
data(2355);
data(52.56);
data("val");
data("kkj");
data(34);
data(3);
data(2);
data(4);
data(5);
data(6);
data(7);
data(8);
display();
}
public static void data(int val){
//add int value to int array
intArray[i] = val;
System.out.println("Int " + intArray[i] + " added to IntArray");
i++;
}
public static void data(Double val){
//add double value to double array
doubleArray[j] = val;
System.out.println("Double " + doubleArray[j] + " added to doubleArray");
j++;
}
public static void data(String val){
//add string value to stringarray
stringArray[k] = val;
System.out.println("String " + stringArray[k] + " added to stringArray");
k++;
}
public static void max(){
//To get the maximum number of values in each array
int x, y, z;
x = y = z = 0;
//counting all the null values in each array and storing in x, y and z
for(m=0;m<10;m++){
if(intArray[m] == 0){
++x;
}
if(doubleArray[m] == 0){
++y;
}
if(stringArray[m] == null){
++z;
}
}
//subtracting the null/0 count from the array size
//this gives the active number of values in each array
x = 10 - x;
y = 10 - y;
z = 10 - z;
//comparing all 3 arrays and check which has the max number of values
//the max numbe is stored in n
if(x > y){
if(x > z){
n = x;
}
else{
n = z;
}
}
else{
if(y > z){
n = y;
}
else{
n = z;
}
}
}
public static void display(){
//printing the arrays in table
//All the null/0 values are excluded
System.out.println("\n\nInt\tDouble\t\tString");
max();
for(m = 0; m < n; m++){
System.out.println(intArray[m] + "\t" + doubleArray[m] + "\t\t" + stringArray[m]);
}
System.out.println("Count : " + m);
}
}
1条答案
按热度按时间xzv2uavs1#
要解决这个问题:
我们可以使用方法重载来捕获所有数据
每个方法将使用不同的数据类型,但名称相同-data()
应该找出每个数组的空值数。
变量n将确定3个整数中最大的一个。
n将是打印表时的测试表达式限制