The way Java iterates through array objects __java

Source: Internet
Author: User

I have summed up three: There are a lot of estimates, just feel these few more commonly used just. You are welcome to discuss the following article:


code doesn't explain.

Be aware of how the code is stored in your computer. Take this code as an example: first you need to know that you perform instances such as Stu[0]=new Student ("John", 18), which creates a Student space inside the heap space to hold various attributes, and stu[0] stores an address that points to that space. So traversal is this address, and as Student stu = new Student ("name", age); This stu is a reference, and you print it out to print this address, and if you print a property, that object has a lot of properties,  Which attribute does the system want to print? (Example one, instance II)


Example One:

public class Student {
	private String name;
	private int age;
	Public Student (String name, int age) {
		this.name=name;
		this.age=age;
	}
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	public int getage () {return age
		;
	}
	public void Setage (int age) {
		this.age = age;
	}
	public void Show () {
		System.out.println (name+ "\ t" +age);
	}
	public static void Main (string[] args) {
		
		Student [] Stu =new student[5];
		Stu[0]=new Student ("John");
		Stu[1]=new Student ("Zhao Si",);
		Stu[2]=new Student ("Harry");
		Stu[3]=new Student ("Old Six");
		Stu[4]=new Student ("Old Seven");
		
		for (int i=0; i<stu.length; i++) {
			stu[i].show ();
		}

	}
}

Example Two:


public class Student {
	private String name;
	private int age;
	Public Student (String name, int age) {
		this.name=name;
		this.age=age;
	}
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	public int getage () {return age
		;
	}
	public void Setage (int age) {
		this.age = age;
	}
	public static void Main (string[] args) {
		
		Student [] Stu =new student[5];
		Stu[0]=new Student ("John");
		Stu[1]=new Student ("Zhao Si",);
		Stu[2]=new Student ("Harry");
		Stu[3]=new Student ("Old Six");
		Stu[4]=new Student ("Old Seven", 22);
		for (int i=0; i<stu.length; i++) {
			System.out.println (stu[i].getname () + "\ T" +stu[i].getage ());}}}
	

Example three: Use to rewrite ToString () is at the bottom of the class to rewrite to in the MyEclipse shortcut key prompt, can be quickly typed


public class Student {
	private String name;
	private int age;
	Public Student (String name, int age) {
		this.name=name;
		this.age=age;
	}
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	public int getage () {return age
		;
	}
	public void Setage (int age) {
		this.age = age;
	}

	public static void Main (string[] args) {
		
		Student [] Stu =new student[5];
		Stu[0]=new Student ("John");
		Stu[1]=new Student ("Zhao Si",);
		Stu[2]=new Student ("Harry");
		Stu[3]=new Student ("Old Six");
		Stu[4]=new Student ("Old Seven");
		
		for (int i=0; i<stu.length; i++) {
		<span style= "White-space:pre" >	</span>system.out.println (Stu[i]);
		}
	
	@Override public
	String toString () {return
		"Student [name=" + name + ", age=" + Age + "]";
	}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.