-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
169 lines (165 loc) · 4.53 KB
/
Array.java
File metadata and controls
169 lines (165 loc) · 4.53 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Java Program to Print the Elements of an Array using for loop
public class GFG {
public static void main(String[] args)
{
int[] arr = { -7, -5, 5, 10, 0, 3, 20, 25, 12 };
System.out.print("Elements of given array are: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
//Adding elements to array by creating large array
import java.util.Arrays;
public class ArrayExample {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6};
int n = arr.length;
int newArr[] = new int[n+1];
int value = 7;
System.out.println(Arrays.toString(arr));
for(int i = 0; i<n; i++) {
newArr[i] = arr[i];
}
newArr[n] = value;
System.out.println(Arrays.toString(newArr));
}
}
//Removing an element from Array using for loop
package com.java;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5};
int[] arr_new = new int[arr.length-1];
int j=3;
for(int i=0, k=0;i<arr.length;i++){
if(i!=j){
arr_new[k]=arr[i];
k++;
}
}
System.out.println("Before deletion :" + Arrays.toString(arr));
System.out.println("After deletion :" + Arrays.toString(arr_new));
}
}
//Searching array elements using binary search
import java.util.*;
public class Gfg {
public static void main(String args[]) throws Exception
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int index = Arrays.binarySearch(array, 2);
if (index >= 0)
System.out.println("Element 2 found at index: "
+ index);
else
System.out.println("Element not found");
}
}
//gettin array element at given index using get() method
import java.lang.reflect.Array;
public class GfG {
public static void main(String[] args)
{
int a[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++) {
int x = (int)Array.get(a, i);
System.out.print(x + " ");
}
}
}
// Java code to demonstrate set() method of Array class
import java.lang.reflect.Array;
public class GfG {
public static void main(String[] args)
{
String s[] = { "Geeks", "is", "Geeks" };
System.out.print("Before Set : ");
for (String x : s) {
System.out.print(x);
}
Array.set(s, 1, "for");
System.out.print("\nAfter Set : ");
for (String x : s) {
System.out.print(x);
}
}
}
//Finding maximum and minimum element in an array
import java.io.*;
class GFG {
static int getMin(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
static int getMax(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = arr.length;
System.out.println("Minimum element of array: " + getMin(arr, n));
System.out.println("Maximum element of array: " + getMax(arr, n));
}
}
// Basic Java program that reverses an array
public class reverseArray {
static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
System.out.println("Reversed array is: \n");
for (int k = 0; k < n; k++) {
System.out.println(b[k]);
}
}
public static void main(String[] args)
{
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}
// Java program to Left Rotate an Array
class GFG {
void leftRotate(int arr[], int d, int n)
{
int temp[] = new int[d];
for (int i = 0; i < d; i++)
temp[i] = arr[i];
for (int i = d; i < n; i++) {
arr[i - d] = arr[i];
}
for (int i = 0; i < d; i++) {
arr[i + n - d] = temp[i];
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
public static void main(String[] args)
{
// Creating an object of class inside main()
GFG rotate = new GFG();
// Custom input array
int arr[] = { 1, 2, 3, 4, 5 };
// Calling method 1 and 2 as defined above
rotate.leftRotate(arr, 2, arr.length);
rotate.printArray(arr, arr.length);
}
}