-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInMixedMatrix.java
More file actions
89 lines (73 loc) · 3.17 KB
/
Copy pathSearchInMixedMatrix.java
File metadata and controls
89 lines (73 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package arrays;
import java.util.Arrays;
public class SearchInMixedMatrix {
public static void main(String[] args) {
// Test cases
testSearchInMatrix(new Object[][] {
{ 1, "3", 5, 7 },
{ 10, "11", 16, "20" },
{ 23, 30, "34", 60 } }, "3"); // Output: true
testSearchInMatrix(new Object[][] {
{ 1, "3", 5, 7 },
{ 10, "11", 16, "20" },
{ 23, 30, "34", 60 } }, 11); // Output: false
testSearchInMatrix(new Object[][] {
{ 1, "3", 5, 7 },
{ 10, "11", 16, "20" },
{ 23, 30, "34", 60 } }, 16); // Output: true
testSearchInMatrix(new Object[][] { {} }, "5"); // Edge case: empty row
testSearchInMatrix(null, 5); // Edge case: null matrix
testSearchInMatrix(new Object[][] { { "1" } }, "1"); // Edge case: single element as string
testSearchInMatrix(new Object[][] { { 1 } }, 1); // Edge case: single element as integer
}
/**
* Searches for a target in a mixed matrix (Strings and Integers).
*
* @param matrix the m x n matrix containing Objects
* @param target the target to search for (Integer or String)
* @return true if the target exists, false otherwise
*/
public static boolean searchMixedMatrix(Object[][] matrix, Object target) {
// Validate input
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
throw new IllegalArgumentException("Matrix cannot be null or empty.");
}
// Iterate through the matrix
for (Object[] row : matrix) {
for (Object element : row) {
// Check for type compatibility and equality
if (element != null && element.equals(target)) {
return true;
}
}
}
return false;
}
/**
* Test helper to validate searchMixedMatrix with various test cases.
*
* @param matrix the matrix to search
* @param target the target to find
*/
public static void testSearchInMatrix(Object[][] matrix, Object target) {
System.out.println("Matrix: " + Arrays.deepToString(matrix));
System.out.println("Target: " + target);
try {
boolean result = searchMixedMatrix(matrix, target);
System.out.println("Result: " + result);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println();
}
}
/* Explanation
1. Handling Mixed Data Types
• The matrix is defined as a 2D array of Object (Object[][]), which can store both String and Integer values.
• The target is also an Object, so it can handle both String and Integer comparisons.
2. Equality Check
• The equals method is used to compare the target with each element in the matrix. This works for both String and Integer types.
• null values are skipped, and elements of unsupported types (if any) are ignored.
3. Edge Case Handling
• The code throws an exception for null or empty matrices.
• Single-element matrices and mixed-type matrices are handled seamlessly. */