array class and methods

ArrayList

Creating Array

int[] my_ar= {34,46,80,55}; // Integer array
String[] my_ar_str= {"One","Two","Three"}; // String array
Using Class , object and Method and returning an array with data.

Array_display

package passing_array;
public class Array_display {
public static void main(String[] args) {
	Array_create obj=new Array_create(); // object created
	
	// displaying all elements of the array
	for (int i = 0; i < obj.my_array_int().length; i++) {
		  System.out.print(obj.my_array_int()[i]+",");
	}
	}
}

Array_create

package passing_array;
public class Array_create {
	public static int[] my_array_int() {
		int[] my_ar= {34,46,80,55};
		return my_ar;
	}
}
Output
34,46,80,55,
Returning string array.

Array_display

package passing_array;
public class Array_display {
public static void main(String[] args) {
	Array_create obj=new Array_create(); // object created
	
	// displaying all elements of the array
	for (int i = 0; i < obj.my_array_str().length; i++) {
		  System.out.print(obj.my_array_str()[i]+",");
	}
	}
}

Array_create

package passing_array;
public class Array_create {
	public static String[] my_array_str() {
		String[] my_ar_str= {"One","Two","Three"};
		return my_ar_str;
	}
}
Using Multidimensional array

Array_display

package passing_array;
public class Array_display {
public static void main(String[] args) {
	Array_create obj=new Array_create(); // object created
	
	// displaying all elements of the array
	for (int i = 0; i < obj.my_array_str().length; i++) {
	 for (int j = 0; j < obj.my_array_str()[i].length; j++){
		  System.out.print(obj.my_array_str()[i][j]+",");
	}
	}
	// Printing single data //
	//System.out.println(obj.my_array_str()[1][1]);
}
}

Array_create

package passing_array;
public class Array_create {
 public static String[][] my_array_str() {
 String[][] my_ar_str= {{"name","dept","age"},{"Alex","Sales","45"}};
 return my_ar_str;
}
}
Output
name,dept,age,Alex,Sales,45,




plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer