forked from NaNaDi/Programming_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics.java
More file actions
52 lines (44 loc) · 1.43 KB
/
Copy pathBasics.java
File metadata and controls
52 lines (44 loc) · 1.43 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
import java.util.Arrays;
public class Basics {
//beginner
//todo: write some methods for some basic operations with arrays, like:
//-return the length of an array
//-drop the last element of an array
//-sum up all the elements of an integer array
//-check if an array contains a given element
public static void main(String[] args) {
int[] myArray = {1,2,3,4,5};
System.out.println("Input array: " + Arrays.toString(myArray));
returnLenghtOf(myArray);
dropLastElementOf(myArray);
sumOf(myArray);
int givenElement = 5;
System.out.println("The array" + (hasGivenElement(myArray, givenElement) ? " " : " not ") + "contains " + givenElement);
}
private static void returnLenghtOf(int[] input) {
System.out.println("length: " + input.length);
}
private static void dropLastElementOf(int[] input) {
int[] newArray = new int[input.length - 1];
for (int index = 0; index < newArray.length; index++) {
newArray[index] = input[index];
}
System.out.println("After drop last element: " + Arrays.toString(newArray));
}
private static void sumOf(int[] input) {
int sum = 0;
for (int index = 0; index < input.length; index++) {
sum += input[index];
}
System.out.println("Sum of all elements: " + sum);
}
private static boolean hasGivenElement(int[] input, int givenElement) {
boolean result = false;
for (int element : input) {
if (element == givenElement) {
result = true;
}
}
return result;
}
}