Java Arrays
To declare an array of integers, you start with:
int[] myArray;
To instantiate an array of ten integers, you can try:
myArray = new int[10];
To set values in that array, try:
myArray[0] = 1; // arrays indices are 0 based in Java
Or at instantiation:
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
To get values from the array, try:
System.out.println(myArray[0]);
To print all the values in an array, try:
// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
System.out.println(myArray2[i]);
}
For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.
Using the Arrays Utility Class
java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.
So you can do things like the following:
// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));
Or
// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);
Answer from justkt on Stack OverflowJava Arrays
To declare an array of integers, you start with:
int[] myArray;
To instantiate an array of ten integers, you can try:
myArray = new int[10];
To set values in that array, try:
myArray[0] = 1; // arrays indices are 0 based in Java
Or at instantiation:
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
To get values from the array, try:
System.out.println(myArray[0]);
To print all the values in an array, try:
// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
System.out.println(myArray2[i]);
}
For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.
Using the Arrays Utility Class
java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.
So you can do things like the following:
// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));
Or
// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);
Well let's say you have an array
int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
And you want to sort it. You do this:
// assumes you imported at the top
Arrays.sort(myArray);
Here's the whole shebang:
import java.util.Arrays;
class ArrayTest {
public static void main(String[] args) {
int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));
}
}
And that results in
C:\Documents and Settings\glow\My Documents>java ArrayTest
[1, 2, 3, 4, 6, 8, 9]
C:\Documents and Settings\glow\My Documents>
Videos
An array is not the same as java.util.Arrays.
Java language has a builtin array type that you can construct with expressions like String[], int[], etc. You don't have to declare anything to use them.
java.util.Arrays is not the type of this kind of object. It is a tool type that contains a lot of methods to be used on arrays: sort them, convert them to and another collection, etc. If you want to use such a class, you need to either use the Fully Qualified Name e.g. java.util.Arrays or declare it as import java.util.Arrays in order to use just Arrays instead of java.util.Arrays every time you need it in a class. The latter approach also makes your code look cleaner and save some typing.
You only need to import java.util.Arrays when you are explicitly using one of the methods that this class provides. All it does is provide useful methods for handling arrays. If you don't call any of them, then you also don't need to import it.
It helps to understand that an import in Java is nothing more than a way to refer to a class by its simple name. In other words it allows you to write Arrays.sort(someArray) instead of having to type java.util.Arrays.sort(someArray).
Hello everyone, my code is below. For some reason I cannot run it. I'm using drjava as that is what's required for my class. Error details below code. Thanks so much in advance. I'm super grateful for this sub.
Code:
===================================================================
import java.util.*;
public class Main{
public static void main(String[] args){
int[] list = {18, 7, 4, 14, 11};
int[] list2 = stretch(list);
System.out.println(Arrays.toString(list)); // [18, 7, 4, 24, 11]
System.out.println(Arrays.toString(list2)); // [9, 9, 4, 3, 2, 2, 7, 7, 6, 5]
}
public static int[] stretch(int[] arr){
int[] arr1 = {};
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr.length * 2; i++){
if (arr[i] % 2 == 0){
arr1[j] = arr[i] / 2;
arr1[j + 1] = arr[i] / 2;
} else {
arr1[j] = arr[i] / 2 + 1;
arr1[j + 1] = arr[i] / 2;
}
return arr;
}
}
}
}
========================================================================
Errors:
==========================================================================
3 errors and 1 warning found:
--------------
*** Errors ***
--------------
File: C:\Users\usr\Main.java [line: 7]
Error: Arrays cannot be resolved
File: C:\Users\usr\Main.java [line: 8]
Error: Arrays cannot be resolved
File: C:\Users\usr\Main.java [line: 12]
Error: This method must return a result of type int[]
-------------
** Warning **
-------------
File: C:\Users\usr\Main.java [line: 15]
Warning: Dead code
======================================================================