Home > Java > javaTutorial > body text

Java object-oriented array information processing

巴扎黑
Release: 2017-06-26 10:57:33
Original
1465 people have browsed it

Although it is a very simple thing, it should be of great help to some self-taught novices and newcomers who do not understand it deeply enough in the early stage.

When I first learned object-oriented, I think many students were confused about this. Simple problems become complicated and unnecessary.

Then look down:

package cn.bdqn.test3;

import java.util.Scanner;

public class Test1 {
public static void main(String[] args) {
//Create two administrator objects
Admin a1 = new Admin();
a1.name = "admin1";
a1.pwd = "111111";

Admin a2 = new Admin();
a2.name = "admin2";
a2.pwd = "222222";

//Will Put the administrator object into the array (object array)
Admin[] admins = {a1,a2};
//Change the administrator password
Scanner input = new Scanner(System.in);
System.out.println("Please enter the user name:");
String name = input.next();
System.out.println("Please enter the password:");
String pwd = input.next();
int index = -1;
for(int i=0;i if(admins[i].name.equals(name) && admins[i].pwd.equals(pwd)){
index = i;
break;
}
}
if(index>=0){
System. out.println("Login successful!");
System.out.println("Please enter a new password:");
String newPwd = input.next();
admins[index].pwd = newPwd;
System.out.println("Password changed successfully, your new password is: "+admins[index].pwd);
}else{
System.out.println("User Incorrect name or password! No permission to update administrator information");
}

}
}

Why do you need to reference objects? It is because when there are a large number of objects that need to be entered If so, you can omit many repeated operations by referencing the object.

Why do you need to create an array for object processing? For objects of the same class, if you need to check the error information, can you do it one by one? To compare?

Understanding helps to learn new knowledge and deepen impressions. If you want to learn java well, you have to think more. I hope everyone will tell me if it is not well written. The reason why I write it is to communicate some experiences with

people and to support some of my own thoughts.

The above is the detailed content of Java object-oriented array information processing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!