if it is, and i wanted to store input from a user into an array without using a java library class, how would i go about doing this? I only know how to use
ArrayList< String > items = new ArrayList< String >();
if it is, and i wanted to store input from a user into an array without using a java library class, how would i go about doing this? I only know how to use
ArrayList< String > items = new ArrayList< String >();
ArrayList is part of Javas Collection framework. Are you instead in search of how to use an array? You may do something like this:
String items = new String[size];
But you need to know how in advance how many items you need to store.
ArrayList<>
is indeed a class from java.util
. However this is not used for arrays, but for some kind of list that can be taken as an array (accessed through indices).
For arrays, you can use String[] myArray = new String[myLength];
The equivalence between ArrayList and arrays would be the following.
Declaration:
ArrayList<String> list = new ArrayList<>();
String[] array = new String[size]; // you have to know the size in advance
Access:
String s = list.get(i);
String s = array[i];
Assignment:
list.add(i, myString);
array[i] = myString;
ArrayList
is a class that comes with Java (its ins the java.util
package). If you dont want to use a class you could use a plain array
. For example to create an array that can hold 10 Strings:
String[] items = new String[10];
You could of course (for educational purpose) write your own class that internally uses an array to hold the values.
Maybe what you are asking for is the arrays like String[]
.
Here some code using these arrays:
String[] myStringArray = new String[size];
String[] otherStringArray = {"one string", "another string", "etc..."};
myStringArray[0] = "this string will be stored in the position 0 of this array";
// assign to sample the value "etc..." stored in the array
String sample = otherStringArray[2];
For more information look at this link: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html