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);
}
Related
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;
}
}
Supposed I have a list of string, I want a DataProvider to produce permutation of 2 of that list to use as 2 parameter in one of the test function. What I have so far is:
#DataProvider(name = "Permutation")
public static Object[][] permutations() {
List <String> permutations = getPermutationList();
Object[][] dataList = new Object[permutations.size()][permutations.size()];
for (int i = 0; i < permutations.size(); i++) {
dataList[i] = new Object[permutations.size()];
for (int j = 0; j < permutations.size(); j++) {
dataList[i][j] = permutations.get(i);
}
}
return dataList;
}
My DataProvider doesn't work as expected, every row is just null.
Any hint please? Thank you
The getPermutationList is the issue with your code. Ensure it's returning the data you are expecting. Below is the same code as above, only with a hardcoded array being initialized. When it runs, there is data in the dataList object.
public static Object[][] permutations() {
List <String> permutations = new ArrayList<String>(Arrays.asList("foo", "bar"));
Object[][] dataList = new Object[permutations.size()][permutations.size()];
for (int i = 0; i < permutations.size(); i++) {
dataList[i] = new Object[permutations.size()];
for (int j = 0; j < permutations.size(); j++) {
dataList[i][j] = permutations.get(i);
}
}
return dataList;
}
I added Strings "Hello", "Cat", "Dog" into the arraylist called values passed it to the method doubleIt() which should return a list of everything doubled, i.e.
"Hello", "Hello", "Cat", "Cat", "Dog", "Dog"
But all Im getting is []. What could I do wrong here ?
import java.util.*;
public class Addition
{
public static void main(String [] args)
{
List<String> values = new ArrayList<String>();
values.add("Hello");
values.add("Cat");
values.add("Dog");
values = doubleIt(values);
System.out.println(values);
}
public static List<String> doubleIt(List<String> values)
{
List<String> temp = new ArrayList<>();
for(int i = 0; i < temp.size(); i++)
{
temp.add(values.get(i*2));
}
return temp;
}
}
Your first mistake is here...
for(int i = 0; i < temp.size(); i++)
temp.size() will be 0 when it's called the first time, you really should be using a values, but this will cause an IndexOutOfBoundsException
So you could use something like...
for (int i = 0; i < values.size(); i++) {
temp.add(values.get(i));
temp.add(values.get(i));
}
instead
First change your for loop condition from
for(int i = 0; i < temp.size(); i++)
to
for(int i = 0; i < values.size(); i++)
and then add values 2 times each.
Your for loop in doubleIt() was looping up to the wrong list size. And you were trying to multiply a string by 2, instead of adding it twice.
public static List<String> doubleIt(List<String> values)
{
List<String> temp = new ArrayList<>();
for(int i = 0; i < values.size(); i++) // <-- you needed to loop up to the size of the values list, not the temp list
{
temp.add(values.get(i));
temp.add(values.get(i));
}
return temp;
}
Ok... So I am in the middle of a project, and I've hit a bit of a wall. If anyone could please explain how to add an integer array to an ArrayList of integer Arrays, I would greatly appreciate it. This is in processing, the javascript version more specifically. I have tested and everything works right up to 'symbols.get(i).add(tempArray). If I print tempArray right before that line it gives me '6 10 16 10 16 20', as it should. And no, it's not just the println(symbols) statement, I also tried just 'println("blah");'and that did not show up in the output, so something is going awry at the .get line.
size(850,250);
String[] myList = new String[100];
ArrayList<Integer[]> symbols = new ArrayList<Integer[]>();
int[] tempArray = new int[];
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
int j = myList.length();
for(int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
symbols.get(i).add(tempArray);
}
println(symbols);
... I have also tried the following in place of 'symbols.get(i).add(tempArray);'
for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i[a]) = tempArray[a];
}
println(symbols);
... I have also tried
for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i) = tempArray[a];
}
println(symbols);
... and
for(int a = 0; a < tempArray.length(); a++)
{
symbols[i][a] = tempArray[a];
}
println(symbols);
I'm out of guesses and tries, any help would be appreciated.
First get the Array of integer by using the get method on the ArrayList, then add [index] to specify what to add at which index of the returned Array.
symbols.get(i)[a] = tempArray[a];
There are some mistakes. The key one is that you can't add int[] to the list expecting Integer[]. Here, see the comments on the code:
void setup()
{
size(850, 250);
String[] myList = new String[100];
ArrayList<Integer[]> symbols = new ArrayList<Integer[]>();
// you can't init an array without a inintial dimension
//int[] tempArray = new int[];
int[] tempArray;
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
//length is not a method... no parenthesis here
//int j = myList.length();
int j = myList.length;
for (int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
// you cant add an int[] to an Integer[] arrayList
// you gotta either change the arraylist type or convert the ints to Integers
// also just use .add(). not get().add() it's a one dimension list
symbols.add(parseArray(tempArray));
println(symbols.get(i) );
println("--");
}
}
//convenience function..
Integer[] parseArray(int[] a) {
Integer[] b = new Integer[a.length];
for (int i = 0; i<a.length; i++) {
b[i] = Integer.valueOf(a[i]);
}
return b;
}
and the other way...
void setup()
{
size(850, 250);
String[] myList = new String[100];
ArrayList<int[]> symbols = new ArrayList<int[]>();
// you can't init an array without a inintial dimension
//int[] tempArray = new int[];
int[] tempArray;
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
//length is not a method... no parenthesis here
//int j = myList.length();
int j = myList.length;
for (int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
symbols.add(tempArray);
println(symbols.get(i) );
println("--");
}
}
i'm out of my league.
ok so if i can do this:
int[]teams = new int[3];
for (int i = 0; i < 3; i++)
{
teams[i] = i;
}
how do i do the similar thing but naming multiple arrays, ie
for (int i = 0; i < 3; i++)
{
int[]i = new int[3];
}
I have read you can't name an array with a variable so I can't just see how to produce arrays with different names (basically more than one) using a loop.
Thanks
You'd do the following (Java):
int teams[][] = new teams[3][3]
You'd do the following (C++):
int** teams = new int*[3];
for(int i=0; i<3; i++){
teams[i] = new int[3];
}
or you could just do
int teams[3][3];
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
teams[i][j] = //whatever you want
}
}
Edit in response to your comment below:
What you're looking for is a MultiMap. There you're going to get:
MultiMap foo = new MultiMap();
foo.put("Bob", 1);
foo.put("Bob", 2);
etc...
You can make an array of arrays (sometimes called a multidimensional array):
int [][] arr = new int[137][42];
You cannot dynamically generate variable names, but you can achieve the same effect with a Map:
//Store mappings from array name (String) to int arrays (int[])
Map<String, int[]> namedArrays = new HashMap<String, int[]>();
for (int i = 0; i < 3; i++)
{
//This is going to be the name of your new array
String arrayName = String.valueOf(i);
//Map an new int[] to this name
namedArrays.put(arrayName, new int[3]);
}
//If you need to access array called "2" do
int[] array2 = namedArrays.get("2")
The advantage of doing it this way is that you can have multiple arrays with same names as long as they are in different maps. But note that when you map two or more arrays to the same name in the same map, the previous array will be overriden (lost).