Get List values To 2D array and Retrive [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I tried to put java list into a 2D array and Retrieve it from the JSP. But It wasn't success.I really need this done.. Support me if you can..I have put my codes below if you able to solve this I'm really appreciate that.
Update:
1st List is getting inserted to first row(I want it insert to first column). likewise other three lists inserted to other 3 rows instead of three columns.
Screenshot
Requirement :
Ex:-
Servlet Code.
Scanner scanner = new Scanner(Result);
List<String> cdLine = new ArrayList<String>();
List<Integer> wtc = new ArrayList<Integer>();
List<Integer> ncc = new ArrayList<Integer>();
List<Integer> ccpps = new ArrayList<Integer>();
ControlData controlData = new ControlData();
while(scanner.hasNextLine())
{
token1 = scanner.nextLine();
Wtcs = controlData.CtrlWeight(token1);
NC = controlData.NofConditions(token1);
Ccspps = controlData.previousComplex(token1);
cdLine.add(token1);
wtc.add(Ccspps);
ncc.add(NC);
ccpps.add(Wtcs);
#SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
}
#SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
scanner.close(); //close the scanner
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/Control_structures.jsp");
request.setAttribute("Code_string", arr);
dispatcher.forward(request, response);
JSP code(using JSTL)
<c:forEach items="${Code_string}" var="post" varStatus="theCount">
<tbody>
<c:forEach items="${post}" var="value" varStatus="cell">
<tr>
<td scope="row">${theCount.count}</td>
<td>${value[0]}</td>
<td>${value[1]}</td>
<td>${value[2]}</td>
<td>${value[3]}</td>
<td>-</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
Update 2:
The 2D array that parsing to the JSP is like this. Hope this also need to be changed.
[[[public class Prime {, if, public static void main(String[]
args) {, , int low = 20, high = 50;, , while (low <
high) {, if(checkPrimeNumber(low)),
System.out.print(low + " ");, , ++low;, }, },
, public static boolean checkPrimeNumber(int num) {,
boolean flag = true;, , for(int i = 2; i <= num/2; ++i) {, ,
if(num % i == 0) {, flag = false;,
break;, }, }, , return flag;, }, }]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]]
Output that I'm getting:
Thank you for your contribution.

There are two problems in your code:
A. With the following statement, you have created a 3-D structure which is an array of arrays of lists (Note that the List is an implementation of a dynamic 1-D array).
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
You need an array of lists as given below:
List arr[]={cdLine,wtc,ncc,ccpps};
B. Do not place the above line inside the while loop. Do it just once after the while loop.
Apart from the points mentioned above, I would recommend you create a custom type instead of using raw type for the List array. Your custom type should be something like:
class MyType {
private List<String> cdLine = new ArrayList<String>();
private List<Integer> wtc = new ArrayList<Integer>();
private List<Integer> ncc = new ArrayList<Integer>();
private List<Integer> ccpps = new ArrayList<Integer>();
// ..constructors and getters and setters
}
Feel free to comment in case of any doubt/issue.

Related

JAVA: How do I merge objects from an arrayList based on a property value?

I'm building (or learning how to) a sports REST API using Spring Boot, Java, and MySQL. I'm building a method that currently takes each match from a collection of matches and returns an ArrayList of TeamStandings for the full list of matches.
Here is the method:
public List<TeamStanding> createStandingsTable(Match[] matches){
List<TeamStanding> teamStandings = new ArrayList<TeamStanding>();
for(int i = 0;i < matches.length; i++) {
TeamStanding firstTeam = new TeamStanding();
TeamStanding secondTeam = new TeamStanding();
//set team ids
firstTeam.setIdTeam(matches[i].getWcmHome());
secondTeam.setIdTeam(matches[i].getWcmAway());
//first team stats
firstTeam.setTeamPlayed((long) 1);
firstTeam.setTeamGoalsFavor(matches[i].getWcmHomeGoals());
firstTeam.setTeamGoalsAgainst(matches[i].getWcmAwayGoals());
firstTeam.setTeamGoalDif(firstTeam.getTeamGoalsFavor() - firstTeam.getTeamGoalsAgainst());
//second team stats
secondTeam.setTeamPlayed((long) 1);
secondTeam.setTeamGoalsFavor(matches[i].getWcmAwayGoals());
secondTeam.setTeamGoalsAgainst(matches[i].getWcmHomeGoals());
secondTeam.setTeamGoalDif(secondTeam.getTeamGoalsFavor() - secondTeam.getTeamGoalsAgainst());
//combined team stats
if(firstTeam.getTeamGoalsFavor() > secondTeam.getTeamGoalsFavor()) {
firstTeam.setTeamWins((long) 1);
firstTeam.setTeamLoses((long) 0);
firstTeam.setTeamDraws((long) 0);
firstTeam.setTeamPoints((long) 3);
secondTeam.setTeamWins((long) 0);
secondTeam.setTeamLoses((long) 1);
secondTeam.setTeamDraws((long) 0);
secondTeam.setTeamPoints((long) 0);
} else if (firstTeam.getTeamGoalsFavor() == secondTeam.getTeamGoalsFavor()) {
firstTeam.setTeamWins((long) 0);
firstTeam.setTeamLoses((long) 0);
firstTeam.setTeamDraws((long) 1);
firstTeam.setTeamPoints((long) 1);
secondTeam.setTeamWins((long) 0);
secondTeam.setTeamLoses((long) 0);
secondTeam.setTeamDraws((long) 1);
secondTeam.setTeamPoints((long) 1);
} else {
firstTeam.setTeamWins((long) 0);
firstTeam.setTeamLoses((long) 1);
firstTeam.setTeamDraws((long) 0);
firstTeam.setTeamPoints((long) 0);
secondTeam.setTeamWins((long) 1);
secondTeam.setTeamLoses((long) 0);
secondTeam.setTeamDraws((long) 0);
secondTeam.setTeamPoints((long) 3);
}
teamStandings.add(firstTeam);
teamStandings.add(secondTeam);
}
return teamStandings;
}
And the result is something like this:
[
{
"idTeam": 7,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 4,
"teamGoalsAgainst": 1,
"teamGoalDif": 3
},
{
"idTeam": 13,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 4,
"teamGoalDif": -3
},
{
"idTeam": 4,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 0,
"teamGoalDif": 1
},
{
"idTeam": 7,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 0,
"teamGoalsAgainst": 1,
"teamGoalDif": -1
}
]
My question is how can I merge these objects based on the idTeam? The result I'm trying to achieve would be to have all the rest of the properties added up while the idTeam remains the same. In the given example the expected one would be:
[
{
"idTeam": 7,
"teamPoints": 3,
"teamPlayed": 2,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 4,
"teamGoalsAgainst": 2,
"teamGoalDif": 2
},
{
"idTeam": 13,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 4,
"teamGoalDif": -3
},
{
"idTeam": 4,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 0,
"teamGoalDif": 1
}
]
Also just a detail, I built the ArrayList of TeamStandings first and now I'm trying to merge them but perhaps I should be stacking them as a loop through the array of Matches, within the same method above but I'm not sure.
Iterate through the list of TeamStanding, mind the team ID and perform the additions. You might want to use the Map to save the pair of team ID as a key and the team itself as a value for easier manipulation. Here is the snipped (I haven't tested it, so you might need to amend it a bit).
List<TeamStanding> list = createStandingsTable(matches);
Map<Integer, TeamStanding> map = new HashMap<>();
for (TeamStanding team: list) {
int id = team.getIdTeam();
if (map.containsKey(id)) {
TeamStanding other = map.get(id);
other.setTeamPoints(team.getTeamPoints());
other.setTeamPlayed(team.getTeamPlayed());
// and so on...
} else {
map.put(id, team);
}
}
List<TeamStanding> merged = new ArrayList<>(map.values());
If you want to create the merged List<TeamStanding> directly from Match[], then you have to use the same idea, however, this might be a bit complicated to combine both of the iterations together. Then I recommend you to stick with these two separate iterations. Brevity, readability and maintainability over performance - moreover, the performance is not really an issue here.
You can use HashMap. Use "idTeam" as key and the object TeamStanding as value. Now you can iterate on the result list and if you find the object in the map, just update its fields and if you don't find then insert the object. After iteration finishes, you can call map.values() and it will give you a collection of objects(TeamStanding) and then you can create a new ArrayList with this collection.
The code will go like as follows:
public List<TeamStanding> mergeTeamStandingList(List<TeamStanding> teamStandingList) {
final Map<Integer, TeamStanding> idTeamVsTeamStandingMap = new HashMap<Integer, TeamStanding>();
teamStandingList.forEach(teamStanding -> {
if(idTeamVsTeamStandingMap.containsKey(teamStanding.getIdTeam())) {
TeamStanding teamStanding1 = idTeamVsTeamStandingMap.get(teamStanding.getIdTeam());
teamStanding1.setTeamDraws(teamStanding1.getTeamDraws() + teamStanding.getTeamDraws());
//so on
} else {
idTeamVsTeamStandingMap.put(teamStanding.getIdTeam(), teamStanding);
}
});
return new ArrayList<>(idTeamVsTeamStandingMap.values());
}
Create a merge method on your Teamstanding object.
public TeamStanding merge(TeamStanding other) {
this.teamPoints += other.getTeamPoints();
this.teamPlayed += other.getTeamPlayed();
this.teamWins += other.getTeamWins();
this.teamDraws += other.getTeamDraws();
this.teamGoalsFavor += other.getTeamGoalsFavor();
this.teamLoses += other.getTeamLoses();
this.teamGoalDif += other.getTeamGoalDif();
return this;
}
Then use Streams to group by teamId and reduce the common items using the merge method.
Map<Integer, Optional<TeamStanding>> mapReduced = teamStandings
.stream()
.collect(groupingBy(TeamStanding::getIdTeam, Collectors.reducing(TeamStanding::merge)));

How can I figure out if two nodes are connected in a graph with recursive depth first search in Java?

Note: the code below now reflects a working solution to the problem, I figured out the error.
I am trying to solve the simple problem of seeing if two nodes are connected. There are many solutions available that use a stack, and I can find much DFS code that is recursive, but non that use recursion and actually search for something and return true/ false. Any help would be appreciated. Thanks!
public static boolean routeBetween(int[][] graph, int startNode, int targetNode){
//where we can keep track of the visited vertices
int numberOfVertices = graph[0].length;
boolean[] visited = new boolean[numberOfVerticies];
//set all verticies to not visited
for(int i=0; i<visited.length; i++){
visited[i] = false;
}
return dfs(graph, visited, startNode, targetNode);
}
//where the actual dfs / recursion will happen, need this to keep track of
//visited
public static boolean dfs(int[][] graph, boolean[] visited, int startNode, int targetNode){
if(startNode == targetNode){
return true;
}
boolean foundNode = false;
if(!visited[startNode]){
visited[startNode] = true;
for(int i=0; i<graph[startNode].length;i++){
if(graph[startNode][i] ==1){
boolean currentChild = dfs(graph, visited, i, targetNode);
foundNode = currentChild || foundNode;
}
}
}
return foundNode;
}
Here is some code that I was using to test the above code:
int [][] matrix = {
{0, 1, 0, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 0},
{1, 1, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 0, 1, 0}
};
System.out.println(GraphTools.routeBetween(matrix,0,1));
System.out.println(GraphTools.routeBetween(matrix,0,2));
I know that you have already figured out your issue, but sometimes it's worthwhile to see things worked out differently.
Since you are already keeping track of all the nodes that you visit in a boolean array, much of the work you do in your dfs method turns out to be redundant.
Another way to do it is as follows:
public static boolean dfs2(int[][] graph, boolean[] visited, int startNode, int targetNode) {
// if you have not visited the current node or the target node
// then visit this node and recursively explore its unvisited
//neighbors
if (!visited[startNode] && !visited[targetNode]) {
// visit the start node
visited[startNode] = true;
for (int i = 0; i < graph[startNode].length; i++) {
if (graph[startNode][i] == 1) {
return dfs(graph, visited, i, targetNode);
}
}
}
// otherwise we just return whether or not we have visited the
// target node and continue... If we have visited the target node
//we never go into the if-statement and we always return true
return visited[targetNode];
}
Your way is perfectly fine, I just wanted to offer an alternative solution. Hope this is helpful.

OLS Multiple Linear Regression with commons-math

Currently I have a dependency to commons-math 2.1 but I want to upgrade it to commons-math 3.6. Unfortunately there are some testcases that are not working any longer. I know what is causing my problem, but I don't know how to change the testcase accordingly to test the correct behavior as before.
I have following test code:
#Test
public void testIdentityMatrix() {
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 } };
double[] y = { 1, 2, 3, 4 };
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
regression.newSampleData(y, x);
double[] b = regression.estimateRegressionParameters();
for (int i = 0; i < y.length; i++)
{
assertEquals(b[i], y[i], 0.001);
}
}
After the upgrade to commons-math 3.6 the OLSMultipleLinearRegression checks the given matrix x and vector y for valid contents. And this validation fails with the message:
not enough data (4 rows) for this many predictors (4 predictors)
What do I need to change to correct that test case?
This is a bug in Commons Math 3.x. When there is no intercept in the model, as long as the design matrix is not singular, the number of observations equal to the number of regressors should be OK. In your example, I think you mean for the third x row to be {0,0,1,0} (otherwise the design matrix is singular). With this change to your data and the code patch applied in the Hipparchus fix your test succeeds. This bug is being tracked as MATH-1392 in Commons Math.
The number of samples has to be bigger than the number of variables. Apparently your test case it not correct. You would have to add at least one more sample.
If you change
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 } };
to
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 }, {1,0,0,0} };
it should work. (although I didn't test it).
I guess the 3rd row of x should be 0010 instead of 0001?
However, if you change x to
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 ), { 0,
0, 0, 1 }, {1,1,1,1} };
and change y to
double[] y = { 1, 2, 3, 4, 10 };
that the last element is the sum of other elements, then it works.

Machine learning on a 8 x 8 matrix - Java

I am trying to implement a machine learning algorithm (k-nn for example).As it stands my Main class, which essentially builds 8×8-pixel matrices into an array to be manipulated later. (See the data description and sample dataset.) As it stands my arrays are printing as a like so:
, Class Code: 7 DataSet:[0, 0, 3, 9, 15, 8, 0, 0, 0, 1, 15, 16, 16, 7, 0, 0, 0, 0, 5, 16, 16, 10, 4, 0, 0, 0, 3, 16, 16, 16, 9, 0, 0, 0, 0, 15, 14, 4, 0, 0, 0, 0, 0, 13, 5, 0, 0, 0, 0, 0, 1, 15, 3, 0, 0, 0, 0, 0, 4, 13, 0, 0, 0, 0, 7]
Now for my starting point I'm looking to try to implement a very basic kNN algorithm as something to build from but I am having trouble manipulating the datasets that are being outputted. I have been reading up on Foundations of Machine Learning by M. Mohri but it hasn't been of any help. My Main class for building my data:
import java.util.*;
import java.io.*;
public class Main {
static class Data {
int[] dataSet;
int classCode;
public Data(int[] dataSet, int label) {
this.dataSet = dataSet;
this.classCode = label;
}
#Override
public String toString() {
return "Class Code: " + classCode + " DataSet:" + Arrays.toString(dataSet) + "\n";
}
}
ArrayList<Data> dataSetList;
int[][] dataArray = new int[2810][65];
private void readFile(String csvFile) {
int instances = 0;
dataSetList = new ArrayList<>();
try {
Scanner scan = new Scanner(new BufferedReader(new FileReader(csvFile)));
while (scan.hasNext()) {
String line = scan.next();
String[] extractedDataFromFile = line.split(",");
for (int i = 0; i < extractedDataFromFile.length; i++) {
dataArray[instances][i] = Integer.parseInt(extractedDataFromFile[i]);
}
dataSetList.add(new Data(dataArray[instances], dataArray[instances][extractedDataFromFile.length - 1]));
instances++;
}
System.out.println(dataSetList.toString());
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
Main main = new Main();
main.readFile("dataset1.csv");
}
}
This is my first time experimenting with machine learning so any feedback or approach to this would be hugely appreciated.
EDIT//
I'm looking at a basic kNN implementation as a starting point whether someone can redirect me to material on implementing on a similar data set or an example using my current provided code. I apologize if my initial post was a little vague

Java - paste a given little array into another bigger array at a specified position (or interval)

I would like to paste a given little array into another bigger array at a specified position (or interval) in Java:
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
Does exist a simple Java method (with matlab it's simple) that helps me pasting "smaller_array" for example at position 2 (of the bigger) so that "bigger_array" variable becomes:
{ 0, 0, 1, 2, 3, 0, 0, 0, 0 };
Please don't paste methods with a simple "for". I want to know if an optimized method exists.
You can use System.arraycopy:
public static void arraycopy(
Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the
specified position, to the specified position of the destination
array. A subsequence of array components are copied from the source
array referenced by src to the destination array referenced by dest.
The number of components copied is equal to the length argument. The
components at positions srcPos through srcPos+length-1 in the source
array are copied into positions destPos through destPos+length-1,
respectively, of the destination array.
Note that the documentation on the System class says (emphasis added):
Among the facilities provided by the System class are standard input,
standard output, and error output streams; access to externally
defined properties and environment variables; a means of loading files
and libraries; and a utility method for quickly copying a portion of
an array.
Here's an exaple and its output:
import java.util.Arrays;
public class ArrayCopyDemo {
public static void main(String[] args) {
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
// start copying from position 1 in source, and into
// position 3 of the destination, and copy 2 elements.
int srcPos = 1, destPos = 3, length = 2;
System.arraycopy(smaller_array, srcPos, bigger_array, destPos, length );
System.out.println( Arrays.toString( bigger_array ));
}
}
[0, 0, 0, 2, 3, 0, 0, 0, 0]
To copy the entire array, just use 0 as srcPos, and src.length as length (where src is the source array; in this case, you'd use smaller_array.length).
You can use System.arraycopy(...). Reusing your example:
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
You could do:
System.arraycopy(smaller_array, 0, bigger_array, 2, 3 );
And you'll end up with your bigger_array modified and now containing the { 0, 0, 1, 2, 3, 0, 0, 0, 0 } you're after.
Use it
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
System.arraycopy(smaller_array, from_position, bigger_array, to_position, smaller_array.length);
I would go for System.arraycopy
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
System.arraycopy(smaller_array, 0, bigger_array, 2, 3 );
System.out.println(Arrays.toString(bigger_array));

Categories