array length
I know it is possible tu increase the array size but i don't know how to do so. The reason why i want to this is because i need to add new items dynamicly to the array. Depending on the lenght of what is typed in.
exemple. If i type the first time 24+13 i want the array to be of length 3 but if i type 12+3-4*5 i want the array to be able to increase to a length of 7 so i can afterwards sum everything.
thank you for the help
[447 byte] By [
wclefa] at [2007-11-26 17:07:00]

Sounds like you need a List instead. http://java.sun.com/docs/books/tutorial/collections/index.html
Aren't these problems usually solved by using a stack?
ok but with the arrayList if i use the add fonction can i still add a new element to a list of size n and by using add it will automaticly do arrayList is now n+1 ?
> ok but with the arrayList if i use the add fonction
> can i still add a new element to a list of size n and
> by using add it will automaticly do arrayList is now
> n+1 ?
Try it:
List<String> list = new ArrayList<String>();
for(int i =0; i<10; i++){
list.add(String.valueOf(i));
}
Arrays are fixed in size; you cannot increase the size once created.ArrayLists will increase their size automatically.Stacks will increse their size automatically.Collections, in general, will grow dynamically.
do i have to do an iterator tu be able to print what is in it ?or there might be another way
> do i have to do an iterator tu be able to print what
> is in it ?
> or there might be another way
If you don't care what the output format is:
System.out.println(list);
For a list containing three Strings "24", "+", "13", that would print:
[24, +, 13]
thank you for the information but this is what i would like to do
if(list.listIterator().hasNext()) {
System.out.println ("element is " + list.iterator().next());
}
but when i do so, if i typed in 1+2 the output will be
Element is 1 and i won't have another output for the Element is + and Element is 2
while(list.iterator().hasNext()){ ... // your code}~Tim
You had:
if(list.listIterator().hasNext()) {
System.out.println ("element is " + list.iterator().next());
}
You don't want to keep getting new iterators. Unless you need to go backwards, an Iterator will work (no need for ListIterator). You also need a loop:
Iterator iter = list.iterator();
while (iter.hasNext())
{
System.out.println ("element is " + iter.next());
}
To format code nicely, use the button above the posting box.
If you are using version 5 or later (didn't I see code with generics earlier?), you can use the enhanced for loop. Behind its synatic sugar, it is calling the iterator() method, defining a local iterator and using its hasNext() and next() methods, but what you see is just this:
import java.util.*;
public class Example {
public static void main(String[] args) {
List<String> words = new ArrayList<String>(Arrays.asList("Hello","world","howdy","doo"));
for(String word : words) {
System.out.println(word);
}
}
}
Tasty...
thx you guys
hehe i'm not to much of a good programmer so bare with me
This is how it is my Array is build in one class i would like to pass this array in parameter to a new class and have this classe do the math
i have an idea of how to reorganise my array and do the math operation but how do i call a class from another classe and have the array i've build pass in parameter. without have a null array
public void SendResult(ArrayList tab[]){
resultats = new Result();
}
this is what i did but it doesnt work
> hehe i'm not to much of a good programmer so bare with me[url= http://www.unitedmedia.com/comics/dilbert/archive/images/dilbert20071832630111.gif]Should I just get naked and sprawl on a desk?[/url] (Image is safe for work!)
Well, you create and instance of that class in the class you will pass from and then provide a suitable method. ie one that takes and argument (and probably returns the same) of an array list in the class you will pass too.
It is just like calling a class with an int or String, set the parameter and pass in the string through the instance.