Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 407fd6e

Browse files
added more programs
1 parent cdc4c07 commit 407fd6e

File tree

135 files changed

+2452
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2452
-0
lines changed

‎CollectionFramworks/ListDemo.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.ArrayList;
2+
3+
public class ListDemo {
4+
public static void main(String[] args) {
5+
6+
ArrayList<String> students = new ArrayList<>();
7+
8+
students.add("alice");
9+
students.add("bob");
10+
students.add("Jasmine");
11+
12+
System.out.println("First student: " + students.get(0));
13+
14+
students.set(1, "Baba");
15+
16+
for (String myStudent : students) {
17+
System.out.println("Student: " + myStudent);
18+
}
19+
20+
System.out.println("Size before removal: " + students.size());
21+
22+
students.remove(2);
23+
24+
System.out.println("Size after removal: " + students.size());
25+
}
26+
}

‎CollectionFramworks/setDemo.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
import java.util.HashSet;
3+
4+
// #setinterface
5+
6+
// > the setinterface(java.util.set) represents a collection with no duplicates
7+
// > ex- Hashset(Unorderd), Treeset(sorted)
8+
9+
10+
public class setDemo{
11+
public static void main(String[] args) {
12+
HashSet<Integer> numbers = new HashSet();
13+
14+
numbers.add(10);
15+
numbers.add(30);
16+
numbers.add(10);
17+
18+
System.out.println("conatians 20?" + numbers.contains(20));
19+
for(Integer num: numbers) {
20+
System.out.println("Numbers: " + num);
21+
}
22+
numbers.remove(10);
23+
System.out.println("Size after removal: " + numbers.size());
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class ArrayIntro{
2+
public static void main(String[] args) {
3+
//Q-Store a roll no
4+
int a = 19;
5+
//q- store a person;s name
6+
String name = "Laksh";
7+
8+
//Q-store 5 roll numbers , store 500 roll numbers
9+
int rollOne = 23;
10+
int rollTwo = 12;
11+
int rollthree = 18;
12+
13+
//Syntax:
14+
// datatype [] variable_name = new datatype[size];
15+
// for storing 5 roll nos now
16+
int [] rnos = new int[5];
17+
// or directly by
18+
int [] rnos2 = {1,2,3,4,5};
19+
for (int i=0;i<rnos2.length;i++){
20+
System.out.println(rnos2[i]);
21+
}
22+
// No mix and match of the data types is allowed in the array in java
23+
// array declaration - int [] ros; >> with this ros is bveing defined in the stack
24+
// int[] ros = new int[5] >> actually here object is being created in the memory (heap) (initialization)
25+
// int[] ros {happens at compile time} : new int[5] {happens at run time} (this is called dynamic memory allocation)
26+
// in java, there's no pointer or something so the java ultimately depends on the jvm for to decide weather elements inside array are continous or not. Now, as per the hava docs we know that -
27+
// 1. array objects are stored inside the heap 2. heap objects are not continous 3. Dynamic memory allocation (run time) 4. So, for the conclusion , if you google array objects are the continous data but in java internally, they are not continous depends on the jvm ultimately
28+
// the new keyword here is used to create an object
29+
// try to ddeclare an array and print the array - you will get 0 at all the indexes when the array is undefined
30+
// but in the case of string array > you get null
31+
//Reason for the null- String is an reference data type in he java . Now, array of string pointing towards indivudal objects in java. So, if nothing is assigned you get null
32+
33+
String [] arr2 = new String[4];
34+
System.out.println(arr2[0]);
35+
System.out.println(rnos[0]);
36+
System.out.println(rnos[1]);
37+
System.out.println(rnos[2]);
38+
System.out.println(rnos[3]);
39+
40+
System.out.println(rnos2[3]);
41+
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class ArrayThree {
5+
public static void main(String[] args) {
6+
Scanner in = new Scanner(System.in);
7+
8+
// array of objects
9+
String[] str = new String[4];
10+
for (int i = 0; i < str.length; i++) {
11+
str[i] = in.next();
12+
}
13+
14+
System.out.println(Arrays.toString(str));
15+
// after modifying the array
16+
str[1] = "Laksh";
17+
System.out.println(Arrays.toString(str));
18+
19+
in.close();
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
3+
public class ArrayTwo {
4+
public static void main(String[] args) {
5+
// taking input from the user
6+
Scanner in = new Scanner(System.in);
7+
8+
int[] originalArr = new int[5];
9+
originalArr[0] = 23;
10+
originalArr[1] = 45;
11+
originalArr[2] = 233;
12+
originalArr[3] = 543;
13+
originalArr[4] = 3;
14+
System.out.println(originalArr[3]);
15+
16+
int[] arr = new int[5]; // array to store user input
17+
18+
// giving the input using the for loops
19+
for (int i = 0; i < arr.length; i++) {
20+
arr[i] = in.nextInt();
21+
}
22+
23+
// enhanced for loop -
24+
for (int num : arr) { //for every element in array, print the element
25+
System.out.print(num + " "); //here num represents every element of an array
26+
}
27+
28+
System.out.println(originalArr[4]); // index out of bound error with this, last digit can be the 5th index
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class MultiDimensional {
2+
public static void main(String[] args) {
3+
// 1 2 3
4+
// 4 5 6
5+
// 7 8 9
6+
int [][] arr = new int[3][];
7+
// no. of rows inseration is mandatory and not the column
8+
int[][] arr2 = {
9+
{1,2,3},
10+
{4,5,6},
11+
{7,8,9}
12+
};
13+
int[][] arr3 = {
14+
{1,2},
15+
{4,5,6},
16+
{7,8,9,10,11}
17+
};
18+
// taking inputs in the 2d array
19+
java.util.Scanner sc = new java.util.Scanner(System.in);
20+
for(int i = 0; i < arr.length; i++) {
21+
System.out.print("Enter number of columns for row " + i + ": ");
22+
int cols = sc.nextInt();
23+
arr[i] = new int[cols];
24+
25+
System.out.println("Enter " + cols + " elements for row " + i + ":");
26+
for(int j = 0; j < cols; j++) {
27+
arr[i][j] = sc.nextInt();
28+
}
29+
}
30+
31+
System.out.println("You entered:");
32+
for(int i = 0; i < arr.length; i++) {
33+
for(int j = 0; j < arr[i].length; j++) {
34+
System.out.print(arr[i][j] + " ");
35+
}
36+
System.out.println();
37+
}
38+
sc.close();
39+
40+
41+
}
42+
}
43+
44+
45+
// Points to remember
46+
// 1. When we create an array (arr) is stored in the stack and the elements ({1,2,3}) are storedi in the heap in java
47+
// 2. An array of arrays
48+
// 3. for ex - arr[1][0] >> [[1,2,3],[4,5,6],[7,8,9]] -> in 2nd row oth index - 4
49+
// 4. Size of the column is variable here , its not fixed
50+
// 5. for ex- [[1,2,3], [4,5],[6,7,8,9]] >> its because , each array in array of arrays is an differnt object
51+
// 6. arr.length >> means the length of rows they are talking about
52+
// 7. When itreating the 2d array the outer for loop iterates over each row and in the inner one over each column of each row
53+
// 8.arr[i].length - this is in the case of mixed no of columns in each row
54+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.ArrayList;
2+
import java.util.Scanner;
3+
4+
public class MyArrayListDemo {
5+
// 1. When you dont know , what will be the size of your array and simply telling java please handle my array size and its elements. There comes the array List in the game
6+
//2. It is similars to vectors in c++, in java it is a part of the collections framework
7+
// 3.Syntax :
8+
// 4. Now, here Integer means , what is the type of the data you want to store in the list, so bascially these are the wrapper classes used in the OOPs. You can include primitives directly here, with wrapper classes <>
9+
10+
public static void main(String[] args) {
11+
Scanner in = new Scanner(System.in);
12+
ArrayList<Integer> list = new ArrayList<>(5);
13+
list.add(78);
14+
list.add(79);
15+
list.add(500);
16+
list.add(45555);
17+
list.add(767);
18+
list.add(786);
19+
list.add(786);
20+
list.add(786);
21+
list.add(786);
22+
23+
// Add as many as you want, no fixed size of the array
24+
// 1.contains function , helps to find if you're searching for is contained in your list- output -true or false
25+
26+
System.out.println(list.contains(34));
27+
// 2. list.set(index, the no. or elements new value)
28+
System.out.println(list.set(1,900 ));
29+
// 3. removing the particular element at a particular index
30+
System.out.println(list.remove(0));
31+
// 4. addding the new elements as an input to the list> getting the items at any index
32+
for(int i=0;i<5;i++){
33+
list.add(in.nextInt());
34+
}
35+
for(int i=0;i<5;i++){
36+
System.out.println(list.get(i)); ///pass index here
37+
}
38+
39+
40+
System.out.println(list);
41+
}
42+
}
43+
44+
45+
// Working of arraylist behind the sscens
46+
// >> arr when you declared it is created in the stack and objects pointing towards the values as reference in the heap
47+
// >>Size is actually fixed> now, when you fill the array to its capacity > a new arraylist is created with double the capacity and old elements copied to that > old one now is empty and gets destroyed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class ObjectArray {
2+
public static void main(String[] args) {
3+
// Step 1: Create an Object array with different data types
4+
Object[] mixedArray = {123, "Java", 45.67, true, 'A'};
5+
6+
// Step 2: Convert each element to String and print it
7+
for (Object element : mixedArray) {
8+
String str = element.toString(); // toString() is available on every Object
9+
System.out.println("Converted to String: " + str);
10+
}
11+
}
12+
}
13+
14+
15+
// Object[] mixedArray = {...};
16+
// This creates an array that can hold any type of data, since everything in Java extends Object.
17+
18+
// Elements include:
19+
20+
// 123 (int, autoboxed to Integer)
21+
22+
// "Java" (String)
23+
24+
// 45.67 (double, autoboxed to Double)
25+
26+
// true (boolean, autoboxed to Boolean)
27+
28+
// 'A' (char, autoboxed to Character)
29+
30+
// 🔸 element.toString()
31+
// Every class in Java inherits the toString() method from the Object class.
32+
33+
// Primitive values like int, boolean, etc., are autoboxed into their wrapper classes (Integer, Boolean), which override toString() to return a readable string form.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.Arrays;
2+
3+
public class PassingInFunctions {
4+
// creating a method
5+
// 1. Strings are immutable in java
6+
// 2. Arrays are mutable in java
7+
// mutablity means, theri values can be modified at the run time
8+
static void change(int [] arr){
9+
arr[0] = 99;
10+
}
11+
12+
public static void main(String[] args) {
13+
int [] nums = {1,2,4,5,6};
14+
System.out.println(Arrays.toString(nums));
15+
change(nums);
16+
System.out.println(Arrays.toString(nums));
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class RandomColumn {
2+
public static void main(String[] args) {
3+
int arr[][] = {
4+
{1,2,3,4},
5+
{5,6},
6+
{7,8,9}
7+
};
8+
9+
for(int row=0; row<arr.length;row++){
10+
for(int col=0; col<arr[row].length;col++){
11+
System.out.print(arr[row][col]+" " );
12+
}
13+
System.out.println();
14+
}
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /