@Test
public void addTest() {
assertEquals(6, MyClass.add(3, 3);
}
public int [] createIntArray (int size) {
return new int [size];
}
String [] sa = new String[2];
sa[0] = "String 0";
sa[1] = "String 1";
String [] sa = {"String 0", "String 1"};
int [] [] a = {{1,2}, {3,4}, {5,6}}; System.out.println(a[1][0]); //prints 3
int [] [] b = new int [5][3];
b[0][2] = 5;
public class Arrays { public static void main(String [] args) { int [] a = {10, 15, 0}; add(a); System.out.println(a[2]); } public static void add(int [] arr) { arr[2] = arr[0] + arr[1]; } }
//a[2] prints as 25
public class Arrays {
public static void main(String [] args) {
int [] a = {10, 15, 99};
add(a);
System.out.println(a[2]);
}
public void add(int [] a) {
a = new int[3];
a[2] = a[0] + a[1];
}
}
//a[2] prints out 99, unaffected by the add function
int a = 5;
int b = 10;
if (a == b) {
System.out.println("same");
} else if (b == a*2) {
System.out.println("double");
} else {
System.out.println("different");
}
int num = 4;
switch(num) {
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
default:
System.out.println("Different");
}
//count up
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
//count down
for(int i = 10; i > 0; i--) {
System.out.println(i);
}
int [] numArray = {1, 2, 3, 4, 5};
for(int number : numArray) {
System.out.println(number);
}
//loops forever
while(true) {
System.out.println("Forever");
}
//loops 10 times
int i = 0;
while(i < 10) {
System.out.println(i);
i++;
}
int [] numbers = {1, 2, 3, 4, 5};
for(int num : numbers) {
if(num ==3) {
break;
}
}
public static boolean hasNumber(int number, int [] numArray) {
for(int arrNum : numArray) {
if(arrNum == number) {
return true;
}
}
return false;
}
int [] numbers = {1, 2, 3, 4, 5};
for(int num : numbers) {
if(num % 2 > 0)
continue;
System.out.println(num + " is an even number");
}