如何在java中对不同的数据类型进行排序并将它们打印到表中(使用方法(重载)

vjhs03f7  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(430)

目标是使用java方法重载输入“任意数据类型”。获取值后,另一种方法将根据数据在不同数组中的数据类型对数据进行排序。所有数据输入并存储在数组中后,必须将其打印到表中。
如: data(23) -这将把整数值存储到数组中 data(34.453) -这将双精度值存储到数组中 data("hello world") -这将把字符串值存储到数组中
考虑到java中的数组大小是预定义的,该表不应打印空值或0值。打印时应排除所有空/0值。

xzv2uavs

xzv2uavs1#

要解决这个问题:
我们可以使用方法重载来捕获所有数据
每个方法将使用不同的数据类型,但名称相同-data()
应该找出每个数组的空值数。
变量n将确定3个整数中最大的一个。
n将是打印表时的测试表达式限制

  1. public class overLoadEg {
  2. //array that will store integers
  3. static int[] intArray = new int[10];
  4. //array that will store doubles
  5. static double[] doubleArray = new double[10];
  6. //array that will store strings
  7. static String[] stringArray = new String[10];
  8. static int i = 0, j = 0, k = 0, m, n;
  9. public static void main(String[] args) {
  10. //input values
  11. data(23);
  12. data(23.4554);
  13. data("Hello");
  14. data("world");
  15. data("help");
  16. data(2355);
  17. data(52.56);
  18. data("val");
  19. data("kkj");
  20. data(34);
  21. data(3);
  22. data(2);
  23. data(4);
  24. data(5);
  25. data(6);
  26. data(7);
  27. data(8);
  28. display();
  29. }
  30. public static void data(int val){
  31. //add int value to int array
  32. intArray[i] = val;
  33. System.out.println("Int " + intArray[i] + " added to IntArray");
  34. i++;
  35. }
  36. public static void data(Double val){
  37. //add double value to double array
  38. doubleArray[j] = val;
  39. System.out.println("Double " + doubleArray[j] + " added to doubleArray");
  40. j++;
  41. }
  42. public static void data(String val){
  43. //add string value to stringarray
  44. stringArray[k] = val;
  45. System.out.println("String " + stringArray[k] + " added to stringArray");
  46. k++;
  47. }
  48. public static void max(){
  49. //To get the maximum number of values in each array
  50. int x, y, z;
  51. x = y = z = 0;
  52. //counting all the null values in each array and storing in x, y and z
  53. for(m=0;m<10;m++){
  54. if(intArray[m] == 0){
  55. ++x;
  56. }
  57. if(doubleArray[m] == 0){
  58. ++y;
  59. }
  60. if(stringArray[m] == null){
  61. ++z;
  62. }
  63. }
  64. //subtracting the null/0 count from the array size
  65. //this gives the active number of values in each array
  66. x = 10 - x;
  67. y = 10 - y;
  68. z = 10 - z;
  69. //comparing all 3 arrays and check which has the max number of values
  70. //the max numbe is stored in n
  71. if(x > y){
  72. if(x > z){
  73. n = x;
  74. }
  75. else{
  76. n = z;
  77. }
  78. }
  79. else{
  80. if(y > z){
  81. n = y;
  82. }
  83. else{
  84. n = z;
  85. }
  86. }
  87. }
  88. public static void display(){
  89. //printing the arrays in table
  90. //All the null/0 values are excluded
  91. System.out.println("\n\nInt\tDouble\t\tString");
  92. max();
  93. for(m = 0; m < n; m++){
  94. System.out.println(intArray[m] + "\t" + doubleArray[m] + "\t\t" + stringArray[m]);
  95. }
  96. System.out.println("Count : " + m);
  97. }
  98. }
展开查看全部

相关问题