Skip to content

Commit

Permalink
added some methods of 2D added
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 committed Jul 28, 2022
1 parent 66818a1 commit b959816
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions arrayspackage/twodimensionalarray.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,48 @@ public void insertelementinarray(int rowindex,int colindex, int value){
System.err.println("Invalid index for array");
}
}

public void accesscell(int rowindex,int colindex){
try{
System.out.println("Accessing row "+rowindex+" and column "+colindex);
System.out.println("cell value is "+arr[rowindex][colindex]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Invalid index");
}
}

public void traverseArray(){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}

public void searchValue(int valueToBeSearched){
try {
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
if(arr[i][j]==valueToBeSearched){
System.out.println(valueToBeSearched+" is present at row "+i+" and column "+j);
return;
}
}
}
System.out.println(valueToBeSearched+" is not found in the array");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public void deletevalue(int rowindex, int colindex){
try {
arr[rowindex][colindex]=Integer.MIN_VALUE;
System.out.println("value deleted successfully at row "+rowindex+" and col "+colindex);
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Invalid index");
}
}
}
6 changes: 6 additions & 0 deletions arrayspackage/twodimensionalarraytest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public static void main(String[] args) {
tda.insertelementinarray(1, 0, 2);
tda.insertelementinarray(1, 1, 3);
System.out.println(Arrays.deepToString(tda.arr));
tda.accesscell(1, 1);
tda.traverseArray();
tda.searchValue(3);
tda.searchValue(5);
tda.deletevalue(1, 1);
tda.traverseArray();
}


Expand Down

0 comments on commit b959816

Please sign in to comment.