Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


jas
ARRAY SIZE!!!
1 Answer(s)      6 years and 3 months ago
Posted in : Java Beginners



Advertisement
View Answers

September 29, 2008 at 5:32 PM


hi

When u don.t know how much element can contain ur array, that time u choose either ArrayList or Vector Class (From Collection Class).

Vector is a growable array.

Here no need to define the intial size of ur array. Runtime it automatic increase its size if or not u declare the size

see the example given below

package rajanikant.code.sep_2008;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Vector;

public class ReadUntilZero {
private double number;
private ArrayList<Double> list = new ArrayList<Double>();

private Vector<Double> list1 = new Vector<Double>();

public void readNumber() throws NumberFormatException, IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter number until Zero : ");
do{
number = Double.parseDouble(br.readLine());
//do your work
if(number == 0.0){
break;
}
list1.add(number); //for vector
list.add(new Double(number)); //arraylist
}while(number !=0);

Collections.sort(list);
System.out.println(list);
}

public static void main(String s[]){
try{
new ReadUntilZero().readNumber();
}catch(Exception e){
System.out.println("IOException Occurs");
}
}

}



Rajanikant


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com