Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-exercise
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
时良荣
java-exercise
Commits
431a2c1a
Commit
431a2c1a
authored
Oct 09, 2020
by
时良荣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
马晓=>JAVA培训List集合
parent
7786caa7
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
276 additions
and
0 deletions
+276
-0
ExerciseFifth.java
src/main/java/com/exercise/ninth/ExerciseFifth.java
+94
-0
ExerciseFirst.java
src/main/java/com/exercise/ninth/ExerciseFirst.java
+21
-0
ExerciseFourth.java
src/main/java/com/exercise/ninth/ExerciseFourth.java
+50
-0
ExerciseSecond.java
src/main/java/com/exercise/ninth/ExerciseSecond.java
+30
-0
ExerciseThird.java
src/main/java/com/exercise/ninth/ExerciseThird.java
+81
-0
Class.class
target/classes/com/exercise/ninth/Class.class
+0
-0
ExerciseFifth.class
target/classes/com/exercise/ninth/ExerciseFifth.class
+0
-0
ExerciseFirst.class
target/classes/com/exercise/ninth/ExerciseFirst.class
+0
-0
ExerciseFourth.class
target/classes/com/exercise/ninth/ExerciseFourth.class
+0
-0
ExerciseSecond.class
target/classes/com/exercise/ninth/ExerciseSecond.class
+0
-0
ExerciseThird.class
target/classes/com/exercise/ninth/ExerciseThird.class
+0
-0
Gender.class
target/classes/com/exercise/ninth/Gender.class
+0
-0
Student.class
target/classes/com/exercise/ninth/Student.class
+0
-0
Student1.class
target/classes/com/exercise/ninth/Student1.class
+0
-0
Worker.class
target/classes/com/exercise/ninth/Worker.class
+0
-0
No files found.
src/main/java/com/exercise/ninth/ExerciseFifth.java
0 → 100644
View file @
431a2c1a
package
com
.
exercise
.
ninth
;
import
java.util.*
;
/**
* 有如下Student 对象,包含三个属性姓名、年龄、分数,班号,
* private String name;
* private int age;
* private int score;
* private String classNum;
* 其中,classNum 表示学生的班号,例如“class05”。 创建集合并完成下列要求:
* 1) 计算所有学生的平均年龄
* 2) 计算各个班级的平均分
*
* 姓名 年龄 分数 班号
* Tom 18 100 class05
* Jerry 22 70 class04
* Owen 25 90 class05
* Jim 30 80 class05
* Steve 28 66 class06
* Kevin 24 100 class04
*
* @author elliot
*/
public
class
ExerciseFifth
{
public
static
void
main
(
String
[]
args
)
{
List
<
Student1
>
student1s
=
new
ArrayList
<>();
student1s
.
add
(
new
Student1
(
"Tom"
,
18
,
100
,
Class
.
class05
));
student1s
.
add
(
new
Student1
(
"Jerry"
,
22
,
70
,
Class
.
class04
));
student1s
.
add
(
new
Student1
(
"Owen"
,
25
,
90
,
Class
.
class05
));
student1s
.
add
(
new
Student1
(
"Jim"
,
30
,
80
,
Class
.
class05
));
student1s
.
add
(
new
Student1
(
"Steve"
,
28
,
66
,
Class
.
class06
));
student1s
.
add
(
new
Student1
(
"Kevin"
,
24
,
100
,
Class
.
class04
));
Iterator
<
Student1
>
student1Iterator
=
student1s
.
iterator
();
double
totalAge
=
0
;
Map
<
Class
,
Integer
>
classN
=
new
HashMap
<>();
Map
<
Class
,
Double
>
classScore
=
new
HashMap
<>();
System
.
out
.
println
(
"所有学生信息"
);
while
(
student1Iterator
.
hasNext
())
{
Student1
student1
=
student1Iterator
.
next
();
totalAge
+=
student1
.
getAge
();
classScore
.
put
(
student1
.
getClassNum
(),
classScore
.
getOrDefault
(
student1
.
getClassNum
(),
0.0
)
+
student1
.
getScore
());
classN
.
put
(
student1
.
getClassNum
(),
classN
.
getOrDefault
(
student1
.
getClassNum
(),
0
)
+
1
);
System
.
out
.
println
(
student1
);
}
// 平均年龄
System
.
out
.
printf
(
"学生平均年龄 %.2f%n"
,
totalAge
/
student1s
.
size
());
// 班级平均分
for
(
Class
cls:
classScore
.
keySet
()){
double
avg
=
classScore
.
getOrDefault
(
cls
,
0.0
)
/
classN
.
getOrDefault
(
cls
,
0
);
System
.
out
.
printf
(
"班级 %s 的平均分为 %.2f%n"
,
cls
,
avg
);
}
}
}
enum
Class
{
class04
,
class05
,
class06
}
class
Student1
{
private
String
name
;
private
int
age
;
private
int
score
;
private
Class
classNum
;
public
Student1
(
String
name
,
int
age
,
int
score
,
Class
classNum
)
{
this
.
name
=
name
;
this
.
age
=
age
;
this
.
score
=
score
;
this
.
classNum
=
classNum
;
}
public
int
getAge
()
{
return
age
;
}
public
int
getScore
()
{
return
score
;
}
public
Class
getClassNum
()
{
return
classNum
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"姓名 %s 年龄 %d 分数 %d 班号 %s"
,
name
,
age
,
score
,
classNum
);
}
}
src/main/java/com/exercise/ninth/ExerciseFirst.java
0 → 100644
View file @
431a2c1a
package
com
.
exercise
.
ninth
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 马晓=>JAVA培训List集合
* 定义一个集合,集合中元素及元素出现的次数为 "a":2,“b”:2,“c”:1,“xxx”:0
* @author elliot
*/
public
class
ExerciseFirst
{
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
stringLists
=
new
ArrayList
<>();
stringLists
.
add
(
"a"
);
stringLists
.
add
(
"b"
);
stringLists
.
add
(
"a"
);
stringLists
.
add
(
"b"
);
stringLists
.
add
(
"c"
);
System
.
out
.
println
(
stringLists
);
}
}
src/main/java/com/exercise/ninth/ExerciseFourth.java
0 → 100644
View file @
431a2c1a
package
com
.
exercise
.
ninth
;
import
java.util.*
;
/**
* 创建一个List,在List 中增加三个工人Worker,基本信息如下:
* 姓名 年龄 工资
* zhang3 18 3000
* li4 25 3500
* wang5 22 3200
* 2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资:3300
* 3) 删除wang5 的信息
* 4) 利用for 循环遍历,打印List 中所有工人的信息
* @author elliot
*/
public
class
ExerciseFourth
{
public
static
void
main
(
String
[]
args
)
{
List
<
Worker
>
workerList
=
new
ArrayList
<>();
workerList
.
add
(
new
Worker
(
"zhang3"
,
18
,
300000L
));
workerList
.
add
(
new
Worker
(
"li4"
,
25
,
350000L
));
workerList
.
add
(
new
Worker
(
"wang5"
,
22
,
320000L
));
System
.
out
.
println
(
workerList
);
workerList
.
add
(
1
,
new
Worker
(
"zhao6"
,
24
,
330000L
));
workerList
.
remove
(
3
);
Iterator
<
Worker
>
iterator
=
workerList
.
iterator
();
while
(
iterator
.
hasNext
())
{
System
.
out
.
println
(
iterator
.
next
());
}
}
}
class
Worker
{
private
String
name
;
private
int
age
;
private
long
salary
;
public
Worker
(
String
name
,
int
age
,
long
salary
)
{
this
.
name
=
name
;
this
.
age
=
age
;
this
.
salary
=
salary
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"姓名 %s 年龄 %d 工资 %.2f"
,
name
,
age
,
salary
/
100.00
);
}
}
src/main/java/com/exercise/ninth/ExerciseSecond.java
0 → 100644
View file @
431a2c1a
package
com
.
exercise
.
ninth
;
import
java.util.*
;
/**
* 定义一个集合, 要求:
* 1)元素及对应元素出现次数分别为 "a":2,“b”:3“c”:1,
* 2)删除重复的元素
* @author elliot
*/
public
class
ExerciseSecond
{
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
stringLists
=
new
ArrayList
<>();
stringLists
.
add
(
"a"
);
stringLists
.
add
(
"b"
);
stringLists
.
add
(
"a"
);
stringLists
.
add
(
"b"
);
stringLists
.
add
(
"b"
);
stringLists
.
add
(
"c"
);
System
.
out
.
println
(
stringLists
);
Iterator
<
String
>
iterator
=
stringLists
.
iterator
();
Set
<
String
>
mySet
=
new
HashSet
<>();
while
(
iterator
.
hasNext
())
{
if
(!
mySet
.
add
(
iterator
.
next
()))
{
iterator
.
remove
();
}
}
System
.
out
.
println
(
stringLists
);
}
}
src/main/java/com/exercise/ninth/ExerciseThird.java
0 → 100644
View file @
431a2c1a
package
com
.
exercise
.
ninth
;
import
java.util.*
;
/**
* 定义一个学生类Student,包含三个属性姓名name、年龄age、性别gender,创建三个学生对象存入ArrayList集合中。
* 姓名 年龄 性别
* 张三 20 男
* 李希 39 男
* 小七 18 女
* 1):遍历集合遍历输出。
* 2):求出年龄最大的学生,然后将该对象的姓名变为:葫芦娃。
* @author elliot
*/
public
class
ExerciseThird
{
public
static
void
main
(
String
[]
args
)
{
List
<
Student
>
students
=
new
ArrayList
<>();
students
.
add
(
new
Student
(
"张三"
,
20
,
Gender
.
MALE
));
students
.
add
(
new
Student
(
"李希"
,
39
,
Gender
.
MALE
));
students
.
add
(
new
Student
(
"小七"
,
18
,
Gender
.
FEMALE
));
ListIterator
<
Student
>
iterator
=
students
.
listIterator
();
int
age
=
0
;
int
index
=
-
1
;
while
(
iterator
.
hasNext
())
{
int
idx
=
iterator
.
nextIndex
();
Student
student
=
iterator
.
next
();
System
.
out
.
println
(
student
);
if
(
student
.
getAge
()
>
age
)
{
age
=
student
.
getAge
();
index
=
idx
;
}
}
students
.
get
(
index
).
setName
(
"葫芦娃"
);
System
.
out
.
println
(
students
.
get
(
index
));
}
}
enum
Gender
{
MALE
(
"男"
),
FEMALE
(
"女"
);
private
final
String
name
;
Gender
(
String
s
)
{
name
=
s
;
}
@Override
public
String
toString
()
{
return
name
;
}
public
String
getName
()
{
return
name
;
}
}
class
Student
{
private
String
name
;
private
int
age
;
private
Gender
gender
;
public
Student
(
String
name
,
int
age
,
Gender
gender
)
{
this
.
name
=
name
;
this
.
age
=
age
;
this
.
gender
=
gender
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"姓名 %s 年龄 %d 性别 %s"
,
name
,
age
,
gender
.
getName
());
}
public
int
getAge
()
{
return
age
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
}
target/classes/com/exercise/ninth/Class.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/ExerciseFifth.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/ExerciseFirst.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/ExerciseFourth.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/ExerciseSecond.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/ExerciseThird.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/Gender.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/Student.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/Student1.class
0 → 100644
View file @
431a2c1a
File added
target/classes/com/exercise/ninth/Worker.class
0 → 100644
View file @
431a2c1a
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment