I have an excell sheet with several colums of values and want to put them in a 2D array. Normally, the way I would do it would be:
int[][] example={{colum 1 values},{colum 2 values},{colum 3 values}};
This way works fine, but can be very time consuming when I have lots of colums. Is there a faster way to do this?
Note: This is being used in an exercise I'm doing, so I dont want to stray into using XML or anything like that just yet.
Without seeing code nobody can really give you specific examples, but the algorithm is pretty simple.
First initialize the outer size of your 2d array. Then you just iterate over your columns in a loop and add the values.
Now, that said, why are you not using a more convenient data structure, such as a List of Lists? (ie List<List<String>>). I would suggest that because it takes all the array management out of the equation.
Note that the code below is somewhat pseudo-code-ish given that we don't know what objects you're working with
List<List<String>> table = new ArrayList<List<String>>();
//Note that you have to initialize each list inside the list
for(int i = 0; i < columns.size; i++)
{
table.add(new ArrayList<String>());
}
then you can just add values to each list:
table.get(columnIndex).add(value);
or
table.get(columnIndex).addAll(listOfValues);
I would like code to store an unknown number of integer values from a website's drop-down list into an array and then compare the values in an existing array with the array values retrieved from the drop-down list.
I was thinking maybe a For loop could work? I apologize but I don't have any sample code to put up because I don't know how to start with creating this code. I do have Basic Java knowledge
You can use arraylists, they are dynamic and don't have to be specified size in advance unlike arrays.
List<Integer> integers = new ArrayList<Integer>();
You can add to integers by integers.add();
I would like to proof whether there is a Float.POSITIVE_INFINITY in my j-th column and row. But I do not know how I can do this, by not having an exploding time complexity. This means I do not want to use for loops for doing this. But I haven't found any alternative way.
Do you know one?
My second thing is I want to copy some columns and/or rows from my 2d array in Java. For rows I can simply use Array[i] to copy it. But for columns I cannot use such form of code. But is there a possibility to do it without for loops?
Basically you have the following options.
Wrap the array into a sepearate class. Use getters and setters where you can check the values when they are assigned and do, whatever you need to do with it.
Loop over the array once it is filled.
After all, there is no way around it that you have to loop if you don't use the first approach. If you have a signal when the array is finished you only have to do it once, but for a more precise approach it would be helpfull to see the code how you fill the array and how you are using the array.
I have a table that I made by taking array list and storing it into another array list and it is composed of strings and doubles. Now I want to apply formulas to the doubles in the Table and I am wondering how to do this. And is there any way for them to update if the numbers in the table change ?
org.apache.poi.ss.usermodel.FormulaEvaluator has an evaluate() method that may be useful in this context. There's an example here.
I'm just asking about this instruction:
String[][] s = new String[2][2];
If i create this variable "s", will i create a string table with 2 lines and 2 columns ?
Or 2 tables with 2 elements?
Thank you for this clarification.
It creates a two-dimensional array with an array of two elements in both dimensions.
Depending on the sole purpose of the variable and the data it actually holds, you're free to interpret the whole whatever way you want. A table (a matrix) with 2 columns and 2 rows is perfectly fine.
Java has no build-in table datatype, the only native basic types of types (metatypes?) are
primitive values (numerics, char and boolean)
arrays (linear indexable collections of variables of the same type).
objects of classes (basically a collection of named variables of specified type, together with some methods, fixed by class). (Strings are examples of this.)
Everything else must be composed of those types. (To complicate this, the array types are all considered subtypes of java.lang.Object, which is the class from which all other classes inherit.)
So you can have arrays of a specific class, classes which have array-types as class variables (fields), and also arrays of arrays, as here.
Your String[][] type consists of arrays of arrays of String, and the new String[2][2] array creation expression creates an array of length two, each element being itself an array of length two. Each element of these arrays can be a string, but on creation it is first null.
s --> [ 0 , 1 ]
| '----> [ null, null ]
|
'---------> [ null, null ]
You now can put in the places of these nulls references to actual String objects, if you want, by using s[0][1] = "Hello"; and similar statements.
As said already by the other answerers, such a 2D-array can be viewed as a table with rows and columns, if you want, but it is nothing special from the language viewpoint.
I think that you are hung up about which is the one true explanation.
In reality, they are are many correct explanations or conceptual models. Which of these is most appropriate / makes the most sense depends on the conceptual level your are thinking at.
At the level of the language / JVM specification you have an array of arrays of strings.
At the syntactic level you have something that looks like a 2-D array of strings, with columns and rows. For a lot of operations it will behave exactly like a 2-D array.
(You can do things to make your String[][] not behave like a conventional 2-D array. For example, you can replace a row to give an "array" in which the rows have different lengths. But that only happens if you "assign" a whole row.)
At the application level what you have can be thought of as a table ... if you want to.
The explanation that you linked to in one of your comments is correct, and so are all of the answers. They are all saying (or trying to say) the same thing.
(BTW - this is what #BalusC's answer is saying. I'm just elaborating.)
So when you ask:
If i create this variable "s", will i create a string table with 2 lines and 2 columns ? Or 2 tables with 2 elements?
... the answer is that it is BOTH of those, depending on your perspective, and how your application uses them. From other perspectives, it is also an array of arrays of strings, or a 2-D array of strings.
You are creating a two dimensional array. This is an array of arrays.
This will technically create an array - of length 2 - of string arrays - also of length 2.
But for all practical purposes it can be thought of as creating a table with 2 rows and 2 columns.
There is no table.
There is an array, an array is simply a reference to the memory where the variables are actually stored.
it will actually create a matrix which is an array of which each element is an array...
array one: [] []
array two: [] []
This is a 2D array. just like writing
String [][] s = new String[2][];
s[0] = new String[2];
s[1] = new String[2];
So I think both your options are correct.