-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoreArray.java
More file actions
173 lines (159 loc) · 4.45 KB
/
Copy pathMoreArray.java
File metadata and controls
173 lines (159 loc) · 4.45 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
170
171
172
173
package com.example.MoreArray;
public class MoreArray {
public String[] posNeg(int n[]){
String a[] = new String[n.length];
for (int i = 0; i <= n.length-1; i++){
if (n[i] > 0){
a[i] = "pos";
}
else if (n[i] < 0){
a[i] = "neg";
}
else{
a[i] = "zero";
}
}
for (int i = 0; i <= n.length-1; i++){
System.out.print(a[i] + " ");
}
System.out.println();
return a;
}
public boolean contain(char n, char m[]){
boolean a = false;
for (int i = 0; i <= m.length-1; i++){
if (m[i] == n){
a = true;
}
}
System.out.println(a);
return a;
}
public int containStr(String n, String m[]){
int a = -1;
for (int i = 0; i <= m.length-1; i++){
if (m[i].equals(n)){
a = i;
}
}
System.out.println(a);
return a;
}
public double minimum(double n[]){
double a = n[0];
for (int i = 0; i <= n.length-1; i++){
if (n[i] < a){
a = n[i];
}
}
System.out.println(a);
return a;
}
public int[] sum(int n[], int m[]){
int a[] = new int[n.length];
for (int i = 0; i <= n.length-1; i++){
a[i] = n[i] + m[i];
}
for (int i = 0; i <= n.length-1; i++){
System.out.print(a[i] + " ");
}
System.out.println();
return a;
}
public void common(int n[], int m[]){
for (int i = 0; i <= n.length-1; i++){
for (int j = 0; j <= m.length-1; j++){
if (n[i] == m[j]){
System.out.print(n[i] + " ");
}
}
}
System.out.println();
}
public void unique(int n[]){
for (int i = 0; i <= n.length-1; i++){
boolean uni = true;
for (int j = i + 1; j <= n.length-1; j++){
if (n[i] == n[j]){
uni = false;
}
}
if (uni == true){
System.out.print(n[i] + " ");
}
}
System.out.println();
}
public int[] digits(int n){
int a = n;
int m = 0;
while (a >= 1){
a = a /10;
m++;
}
int b[] = new int[m];
for (int i = m-1; i >= 0; i--){
b[i] = n % 10;
n = n / 10;
}
for (int i = 0; i <= m-1; i++){
System.out.print(b[i] + " ");
}
System.out.println();
return b;
}
public int[] noDuplicates(int n[]){
int num = 0;
int seq = 0;
for (int i = 0; i <= n.length-1; i++){
boolean uni = true;
for (int j = i + 1; j <= n.length-1; j++){
if (n[i] == n[j]){
uni = false;
}
}
if (uni == true){
num++;
}
}
int clean[] = new int[num];
for (int i = 0; i <= n.length-1; i++){
boolean uni = true;
for (int j = i + 1; j <= n.length-1; j++){
if (n[i] == n[j]){
uni = false;
}
}
if (uni == true){
clean[seq] = n[i];
seq++;
}
}
for (int i = 0; i <= clean.length-1; i++){
System.out.print(clean[i] + " ");
}
return clean;
}
public static void main(String[]args){
MoreArray runner = new MoreArray();
int a[] = {-4,6,2,-1,-6,9};
char b[] = {'i','g','o','d'};
String c[] = {"man", "CS", "rocks"};
double d[] = {2, 3, 1, 4, 5};
int e[] = {5, 3, 7};
int f[] = {3, 8, 9};
int g[] = {4,1,7,3,4};
int h[] = {8,2,3,4};
int k[] = {6, 6, 3, 9, 4, 3, 2};
int l[] = {5, 3, 5, 7, 2, 3};
runner.posNeg(a);
runner.contain('c', b);
runner.containStr("CS", c);
runner.minimum(d);
runner.sum(e, f);
runner.common(g, h);
runner.unique(k);
runner.digits(56723);
runner.noDuplicates(l);
}
}