This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String arr[][] = new String[3][]; | |
arr[0] = new String[3];//3 columns | |
arr[1] = new String[2];//2 columns | |
arr[2] = new String[5];//5 columns | |
for (int i = 0; i < arr.length; i = i+1){ | |
//each row has a diff column | |
for (int j = 0; j < arr[i].length ; j = j+1){ //NOTICE: arr[i].length | |
arr[i][j] = i + "" + j + " "; | |
System.out.print(arr[i][j]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declaring a Rectangular Matrix | |
String arr[][] = new String[6][7]; | |
int row = arr.length;// Row = 6 | |
int col = arr[0].length; // Columns = 7 | |
//Printing the Matrix using FOR Loop | |
for (int i = 0; i < row; i = i + 1) { | |
for (int j = 0; j < col; j = j + 1) { | |
arr[i][j] = i + "" + j + " "; | |
System.out.print(arr[i][j]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Change all nodes to 42 */ | |
ListNode runner = list; | |
//Change values as itrarte | |
while (runner != null){ | |
runner.data = 42; | |
runner = runner.next; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Add nodes with 42 in the end */ | |
public void addNodeEnd{ | |
ListNode runner = list; | |
while(runner.next != null){ | |
runner = runner.next; | |
} | |
runner.next = new ListNode(42, null); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void set(int index, int value){ | |
ListNode runner = front; | |
while(index != 0){ | |
runner = runner.next; | |
index--; | |
} | |
runner.data = value; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int min(){ | |
if (front == null) | |
throw new NoSuchElementException(); | |
int min = 2147483647;// Max int value | |
ListNode runner = front; | |
while (runner != null){ | |
if (runner.data < min){ | |
min = runner.data; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int lastIndexOf(int num){ | |
if (front == null) | |
return -1; | |
int index = 0; | |
int lastIndex = -1; | |
ListNode runner = front; | |
while (runner != null){ | |
if (runner.data == num){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int countDuplicates(){ | |
if (front == null || front.next == null) | |
return 0; | |
int duplicateCount = 0; | |
int temp = front.data; | |
ListNode runner = front.next; | |
while (runner != null){ | |
if (temp == runner.data){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int deleteBack(){ | |
if (front == null) | |
throw new NoSuchElementException(); | |
// Only one node is present | |
if (front.next == null){ | |
int deletedData = front.data; | |
front = null; | |
return deletedData; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Traditional Iteration for LinkedList | |
LinkedList runner = head; | |
while(runner != head){ | |
System.out.println(runner.data); | |
runner = runner.next; | |
} | |
// The for loop | |
for (LinkedList runner = head; runner != null; runner = runner.next{ | |
System.out.println(runner.data); |
OlderNewer