Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How could I do this?

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How could I do this?

    I want to write a simple program that creates 3 arrays with about 10 elements that each hold an integer value, and then print out the values at each array element by going through a loop.

    I'd like the output to be something like:

    first array values: 1,2,3,4,5,6,7,8,9,10
    second array values: 1,2,3,4,5,6,7,8,9,10
    third array values:1,2,3,4,5,6,7,8,9,10


    How should I go about doing this? (In java of course)

  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: How could I do this?

    Are you having a specific problem? Can you post the code that you have tried to write and any specific problems you're having and we'll try to help.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How could I do this?

    Sure. I am trying to create arrays outside the main function and make them private too. I want to use functions to get and set the values of the array elements....I tried this so far but I'm not sure what to do next:
    [code]
    public class Lists2{
    int _ilist[] = {2,4,6};
    double _dlist[] = new double[3];
    String _slist[] = new String[2];

    public void set_dlist(double[] da){
    _dlist = da;
    }
    public double [] get_dlist(){
    return _dlist;
    }

    public static void main(String[] args){

    Lists2 listr = new Lists2;

    System.out.println("\n_slist = " + _slist);

    for(int i=0; i <_slist.length; i++){
    System.out.println("\n_slist[" + i +"] = " + _slist[i]);
    }

    for(int i=0; i <_ilist.length; i++){
    System.out.println("\n_ilist[" + i +"] = " + _ilist[i]);
    }

    for(int i=0; i <_dlist.length; i++){
    System.out.println("\n_dlist[" + i +"] = " + _dlist[i]);
    }

    _dlist[0] = 1.0;
    _dlist[1] = 4.0;
    _dlist[2] = 4.0;
    for(int i=0; i<_dlist.length; i++){
    if(i>0){
    if(_dlist[i] == _dlist[i-1]){
    System.out.println("\n_dlist[" + i + "] matches _dlist[" + (i-1) + "]");
    }
    }
    }
    }
    }
    [code]

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How could I do this?

    HI... try this with the following piece of code. And call the method from another class

     
    public class Lists {
     
    	int [] _Array1; 
    	int [] _Array2; 
    	int [] _Array3; 
     
    	public Lists(int [] Array1, int [] Array2, int [] Array3){
    		this._Array1 = Array1;
    		this._Array2 = Array2;
    		this._Array3 = Array3;
     
    	}
     
     
    	public void printArray(){
    		System.out.println("Printing the first Array");
     
    		for(int i=0;i<_Array1.length;i++){
    			System.out.println(_Array1[i]);
    		}
    		System.out.println("Printing the second array");
    		for(int i=0;i<_Array2.length;i++){
    			System.out.println(_Array2[i]);
    		}
    		System.out.println("Printing the 3rd Array");
    		for(int i=0;i<_Array3.length;i++){
    			System.out.println(_Array3[i]);
    		}
    	}
     
     
    }