Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FirefighterStarter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OUALAN Yanis
FirefighterStarter
Commits
bd34ce06
Commit
bd34ce06
authored
7 months ago
by
Yanis O
Browse files
Options
Downloads
Patches
Plain Diff
[Modif] Changement de Matrix, afin de supporter les matrices non carré
parent
5f0836f8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/util/Matrix.java
+69
-6
69 additions, 6 deletions
src/main/java/util/Matrix.java
with
69 additions
and
6 deletions
src/main/java/util/Matrix.java
+
69
−
6
View file @
bd34ce06
...
...
@@ -6,26 +6,89 @@ import java.util.NoSuchElementException;
public
class
Matrix
<
E
>
implements
Iterable
<
E
>
{
private
ArrayList
<
ArrayList
<
E
>>
matrix
;
private
final
int
rows
;
private
final
int
columns
;
public
Matrix
()
{
this
.
matrix
=
new
ArrayList
<
ArrayList
<
E
>>();
public
Matrix
(
int
rows
,
int
columns
)
{
this
.
rows
=
rows
;
this
.
columns
=
columns
;
this
.
matrix
=
new
ArrayList
<>(
rows
);
// Initialiser chaque ligne de la matrice
for
(
int
i
=
0
;
i
<
rows
;
i
++)
{
ArrayList
<
E
>
row
=
new
ArrayList
<>(
columns
);
// Initialiser chaque colonne avec des valeurs nulles
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
row
.
add
(
null
);
}
this
.
matrix
.
add
(
row
);
}
}
public
E
get
(
int
x
,
int
y
)
{
validateIndexes
(
x
,
y
);
return
matrix
.
get
(
x
).
get
(
y
);
}
public
E
set
(
int
x
,
int
y
,
E
object
)
{
validateIndexes
(
x
,
y
);
return
matrix
.
get
(
x
).
set
(
y
,
object
);
}
public
void
clear
()
{
this
.
matrix
=
new
ArrayList
<
ArrayList
<
E
>>();
this
.
matrix
.
clear
();
for
(
int
i
=
0
;
i
<
rows
;
i
++)
{
ArrayList
<
E
>
row
=
new
ArrayList
<>(
columns
);
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
row
.
add
(
null
);
}
this
.
matrix
.
add
(
row
);
}
}
public
int
size
()
{
return
matrix
!=
null
?
matrix
.
get
(
0
).
size
()
*
matrix
.
size
()
:
0
;
return
rows
*
columns
;
}
public
int
getColumns
(){
return
this
.
columns
;
}
public
int
getRows
(){
return
this
.
rows
;
}
public
void
displayMatrix
()
{
System
.
out
.
print
(
" "
);
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
System
.
out
.
print
(
"___ "
);
}
System
.
out
.
println
();
for
(
int
i
=
0
;
i
<
rows
;
i
++)
{
System
.
out
.
print
(
"| "
);
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
if
(
matrix
.
get
(
i
).
get
(
j
)
!=
null
)
{
System
.
out
.
print
(
" x | "
);
}
else
{
System
.
out
.
print
(
" | "
);
}
}
System
.
out
.
println
();
System
.
out
.
print
(
" "
);
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
System
.
out
.
print
(
"___ "
);
}
System
.
out
.
println
();
}
}
private
void
validateIndexes
(
int
x
,
int
y
)
{
if
(
x
<
0
||
x
>=
rows
||
y
<
0
||
y
>=
columns
)
{
throw
new
IndexOutOfBoundsException
(
"Indices x: "
+
x
+
" y: "
+
y
+
" hors limites pour la matrice."
);
}
}
@Override
public
Iterator
<
E
>
iterator
()
{
...
...
@@ -38,7 +101,7 @@ public class Matrix<E> implements Iterable<E> {
@Override
public
boolean
hasNext
()
{
return
row
<
matrix
.
size
()
&&
col
<
matrix
.
get
(
row
).
size
()
;
return
row
<
rows
&&
col
<
columns
;
}
@Override
...
...
@@ -48,7 +111,7 @@ public class Matrix<E> implements Iterable<E> {
}
E
element
=
matrix
.
get
(
row
).
get
(
col
);
col
++;
if
(
col
>=
matrix
.
get
(
row
).
size
()
)
{
if
(
col
>=
columns
)
{
col
=
0
;
row
++;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment