-1

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 >();
user3452963
  • 117
  • 2
  • 4
  • 15
  • then you have to create your own arraylist. – Salah Mar 24 '14 at 09:59
  • Using the `Arrays` class. – AntonH Mar 24 '14 at 10:01
  • Why do you not want to use a Java library class? – AntonH Mar 24 '14 at 10:02
  • Instead of using arrays, you can write your own Linked List. If you've never done that, it's quite instructive. And for me personally it would be more fun than trying to use Java's terrible `Array`s. – CompuChip Mar 24 '14 at 10:03
  • This question appears to be off-topic because it is can be answered in 10 seconds by looking at the javadoc index. And it is highly unlikely to help future readers. – Stephen C Mar 24 '14 at 10:23

4 Answers4

0

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.

Harmlezz
  • 7,972
  • 27
  • 35
0

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;
Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • can you give me a short example of how to take in the users input of 1 string, 1 int and 1 double and store it in this array? String[] myArray = new String[3]; – user3452963 Mar 24 '14 at 10:07
  • If the different elements input by the user are not of same type, then an array is probably not the way to go. Why don't you just use 3 variables? – Joffrey Mar 24 '14 at 10:08
  • i actually want to input all 3 types and use them further in the program. if using an array is not the way, how else would i do that? do i need 3 different arrays? like `code` String[] myArray & int[] myArray & double[] myArray? – user3452963 Mar 24 '14 at 10:12
  • Just 3 fields: `String s`, `int i`, and `double d`. No need for arrays if you don't have several values of each type. – Joffrey Mar 24 '14 at 10:13
  • no, but I do. essentially my program needs 4 strings from the user, 2 ints and 1 double. I then need to use all 7 in the program. – user3452963 Mar 24 '14 at 10:14
  • Alright, alright. What the language *can* do is to store several values of same type in an array. So 2 arrays for you (one for the `String`s, the other for the `int`s), and just a variable for the `double`. – Joffrey Mar 24 '14 at 10:18
  • However, you should wonder if the values of same type are actually related. For instance, if your ints are dimensions (width, length), or a sequence of elements having the same meaning, then an array would make sense, but if one is a number of lines, and the other is the number of bananas I wanna eat, then don't use an array, use 2 variables. – Joffrey Mar 24 '14 at 10:19
  • alright. so one for String[] myArray & int[] myArray and then just a variable for a double? if so, like i asked before, how can i get the input from a user and store them in their respective arrays? can you give me an example, please? – user3452963 Mar 24 '14 at 10:19
0

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.

SebastianH
  • 2,172
  • 1
  • 18
  • 29
0

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

Tony_craft
  • 213
  • 1
  • 10