Two dimension array, how to programically add new array element? Java - java

I don't know, if I forgot how, or I just can't figure it out how.
For example :
Object[][] data = {
{"id", "projectname","valueid", "value"},
};
And this is how they should be added, but in loop:
Object[][] data = {
{"id", "projectname","valueid", "value"},
{"id2", "projectname2","valueid2", "value2"},
{"id3", "projectname3","valueid3", "value3"},
};
And so on..
I need a tip only, like a skeleton how it should be. I tried to figure it out, but had no idea how.
Thanks!

You can add a new array to another array like this :
data[1] = new Object[]{"id_1", "projectname_1","valueid_1", "value_1"};
...
data[n] = new Object[]{"id_n", "projectname_n","valueid_n", "value_n"};
You can use this way in any loop for example :
int length = 5;
Object[][] data = new Object[length][];
for(int i = 0; i < length; i++){
data[i] = new Object[]{...some information};
}

for (int i = 1; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
int line = i+1;
data[i][j] = data[0][j]+ line;
}
}

Related

Why are all of the values in my array null?

Can anyone tell me why in the second array I am getting null instead of the information from the file?
Here is my code in the main method:
...OTHER CODE
String[] fileDataNames = new String[10];
for (int j = 0; j < 10; j++) {
fileDataAll[j][0] = fileDataNames[j];
}
System.out.println(Arrays.deepToString(fileDataNames));
Change below from :
fileDataAll[j][0] = fileDataNames[j];
to
fileDataNames[j] = fileDataAll[j][0];
You invert the operation, it's the value from fileDataAll that needs to go to fileDataName
for (int j = 0; j < NUMBER_OF_VOLUNTEERS; j++) {
fileDataNames[j] = fileDataAll[j][0];
}

Displaying the string values of 2 dimensional Object array

I am trying to display the contents of an array after iterating through rows and columns of a JTable. I tried Arrays.toString(myTwoDimensionalArrayVariable) but it won't display the string values.
My goal is to check duplicates for every column per row of a destination JTable when user tries to add row values from a source JTable that's why I want to display the contents of the array.
The values on columns are combination of double, String, and int.
int myRowCount = aJTableParameter.getRowCount();
int myColumnCount = aJTableParameter.getColumnCount();
Object[][] myRowValues = new Object[myRowCount][myColumnCount];
for (int j = 0; j < myRowCount; j++) {
for(int i = 0; i< myColumnCount; i++){
myRowValues[j][i] = aDestinationTable.getValueAt(j, i);
}
}
System.out.println(Arrays.toString(myRowValues));
if (Arrays.asList(myRowValues).contains(column1Value)
&& Arrays.asList(myRowValues).contains(column2Value)
&& Arrays.asList(myRowValues).contains(column3Value)
&& Arrays.asList(myRowValues).contains(column4Value)) {
JOptionPane.showMessageDialog(null, "Duplicate, try again.");
}else{
//do something else
}
I only get this output:
run:
Successfully recorded login timestamp
[]
[[Ljava.lang.Object;#35fa3ff2]
[[Ljava.lang.Object;#407c448d, [Ljava.lang.Object;#1e78a60e]
Is there any other alternative than using 2 Dimensional Arrays?
I'd appreciate any help.
Thanks.
IFF your JTable cells contain only Strings, you can define your array as String[][] instead of Object[][] and fill it with your JTable contents using aDestinationTable.getValueAt(j, i).toString().
EDIT: since that's not the case (as per your comment), it's probably better to use a List, like this:
List<List<Object>> objectList = new ArrayList<>();
for (int j = 0; j < 2; j++) {
objectList.add(j, new ArrayList<>());
for (int i = 0; i < 2; i++) {
if (i==0) objectList.get(j).add("string" + j + i);
if (i==1) objectList.get(j).add((double) 37.8346 * j * i);
}
}
System.out.println("OBJECT LIST: "+objectList);
Output:
OBJECT LIST: [[string00, 0.0], [string10, 37.8346]]
Your code should look like this, then:
List<List<Object>> myRowValues = new ArrayList<>();
for (int j = 0; j < myRowCount; j++) {
myRowValues.add(j, new ArrayList<>());
for (int i = 0; i < myColumnCount; i++) {
myRowValues.get(j).add(aDestinationTable.getValueAt(j, i));
}
}
System.out.println(myRowValues);

Arraylist<String[]> how to access?

I have an ArrayList< String[] >, but how do you get String[] back to normal values?
This is what I got (of course not finished), and I want it to return an Object[][] with my values. Is it possible?
public Object[][] noName(String[] col) {
ArrayList<String[]> arrlist = new ArrayList<String[]>();
for (int i = 0; i < col.length; i++) {
arrlist.add(createColumnList(col[i]));
}
// How to get it into an Object[][]?
// Something made up..
return new Object[0][0];
}
createColumnList is just a method to create an array from different columns in a XML
It's a one-liner, actually:
return arrlist.toArray(new Object[arrlist.size()][]);
But it would be even simpler (and more efficient) to start with the array directly:
final Object[][] ret = new Object[col.length][];
for (int i = 0; i < col.length; i++) ret[i] = createColumnList(col[i]);
return ret;
Try it
final Object[][] obj = new Object[col.length][];
for (int i = 0; i < col.length; i++) obj[i] = createColumnList(col[i]);
return obj;
You just to loop and Object array to store. Here is the code to add to your comment place:
List<String[]> arrList = new ArrayList<String[]>();
Object[][] objArray = new Object[ arrList.size() ][ col.length ];
for ( int i = 0; i < arrList.size(); i++ )
objArray[ i ] = arrList.get( i );
I think that will be what you want.
First of all there is an .toArray function for Arraylist.
alternatifly you can do this.
Object[][] objarr = new Object[arrlist.size()][arrlist.get(0).length]
for(int i=0;i<arrlist.size();i++){
for(int j=0;j<arrlist.get(i).size(),j++){
objarr[i][j]=arrlist.get(i)[j];
}
}
This simply loops over every element in the array and puts them in your new array. Chance Object[][] into String[][] if you want to put it in a String array instead.
I think this should also be possible but am a bit fuzy on the rules of recasting arrays.
Object[][] objarr = new Object[arrlist.size()][];
for(int i=0;i<arrlist.size();i++){
objarr=arrlist.get(i);
}

Create values for each time the for loop loops

So I'm trying to create a for loops that creates a new value each times it runs.
Something like this:
for(int i = 0; i < con.length; i++) {
Vector max[i] = new Vector(i+1, i+2, i+3);
}
I'm kind of new to java, so i don't understand it very well.
Thanks for your help.
This should work
Vector[] max = new Vector[con.length];
for(int i = 0; i < con.length; i++) {
max[i] = new Vector(i+1, i+2, i+3);
}

compare two array of string and store the result in another array

I want to compare two arrays and store the difference in another array
For example the two arrays might be
String[] a1 = { "cat" , "dog" };
String[] a2 = { "cat" , "rabbit" };
The resultant array would be like this
{ "rabbit" }
I use this code, but it does not work
int n = 0;
for (int k = 0; k <= temp.length; k++)
{
for (int u = 0; u <= origenal.length; u++)
{
if (temp[k] != origenal[u] && origenal[u] != temp[k])
{
temp2[n] = temp[k];
System.out.println(temp[u]);
n++;
}
}
}
This should do the trick.
String[] result = new String[100];
Int k = 0;
Boolean test = true;
for(i=0; i < a1.length; i++){
for(j=0; j < a2.length; j++){
if(a2[i].equals(a1[i])) continue;
test = false
}
if(test == false) result[k++] = a1[i];
}
I think that this may be what you are looking for. Note that it will only add to the third 'array' if the value exist in second array but not in first. In your example only rabbit will be stored, not dog (even though dog does not exist in both). This example could possibly be shortened but I wanted to keep it like this so it is easier to see what is going on.
First import:
import java.util.ArrayList;
import java.util.List;
Then do the following to populate and analyze the arrays
String a1[] = new String[]{"cat" , "dog"}; // Initialize array1
String a2[] = new String[]{"cat" , "rabbit"}; // Initialize array2
List<String> tempList = new ArrayList<String>();
for(int i = 0; i < a2.length; i++)
{
boolean foundString = false; // To be able to track if the string was found in both arrays
for(int j = 0; j < a1.length; j++)
{
if(a1[j].equals(a2[i]))
{
foundString = true;
break; // If it exist in both arrays there is no need to look further
}
}
if(!foundString) // If the same is not found in both..
tempList.add(a2[i]); // .. add to temporary list
}
tempList will now contain 'rabbit' as according to the specification. If you necessary need it to be a third array you can convert it to that quite simply by doing the following:
String a3[] = tempList.toArray(new String[0]); // a3 will now contain rabbit
To print the content of either the List or Array do:
// Print the content of List tempList
for(int i = 0; i < tempList.size(); i++)
{
System.out.println(tempList.get(i));
}
// Print the content of Array a3
for(int i = 0; i < a3.length; i++)
{
System.out.println(a3[i]);
}

Categories