Putting values into a two-dimensional array from an Excell sheet - java

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);

Related

Creating a Sorted 2D Array from custom Object properties

I have a List of objects created from a map like so:
Map incomingRequest = (Map)object;
List accounts = (List)incomingRequest.get("accountList");
In addition, I loop through these objects and pull them out one by one via the index of that list like so and create the account object:
for (int accountRow = 0; accountRow < accounts.size(); accountRow++){
Account account = (Account)accounts.get(accountRow);
There is a method on this Account account object that I can use to get an identifier I can sort on called like so: account.getComp_id().getIcLine(). This gives me a non-unique number that I can use to group with. I now have a need to do some calculations involving only the grouping ov like IcLine properties.
My thought is to create a 2D ArrayList so that I can loop through each sorted array of objects sharing the same IcLine number. However, I currently can't figure out exactly how I would do that after googling around and trying to work through it. I feel like this is a good job for recursion, but I can't figure out how to create the 2D ArrayList I need. Your guidance is appreciated

Number of column in a two dimensional ArrayList

How do I determine which column in a double ArrayList has the same value in all rows? My ArrayList looks like the image below.
Update: Seems like there are many where were confused by the question. I should have asked in a more eloquent way.
I have a two dimensional ArrayList which consists of ArrayLists. I have a certain number of rows and columns in this two dimensional ArrayList. I would like to create a method that would return how many numbers of columns are in that ArrayList. That was what I meant in my original question. Now I know how to create this method as I got help from some of the answers below.
I suppose you have an ArrayList of ArrayLists, then you can do this to retrieve number of "columns":
list.get(0).size()
It means get the first ArrayList from list and get it's size.
Assuming that you just have nested ArrayLists, and the number of rows is represented by the "top" ArrayList size, you can just get the size of the first nested one:
aList.get(0).size();
...assuming of course that aList is your top most ArrayList.
Do you mean you have a
List<List<MyType>> listOfList = ...
and you want to know the size of the inner dimension?
int width = listOfList.get(0).size();
assuming all lists/rows are the same size/width.

Reading an unknown number of items in a web drop down list and storing in an array

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();

2d array copy column and row and proof if the contains is equal to or not

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.

Adding elements into ArrayList at position larger than the current size

Currently I'm using an ArrayList to store a list of elements, whereby I will need to insert new elements at specific positions. There is a need for me to enter elements at a position larger than the current size. For e.g:
ArrayList<String> arr = new ArrayList<String>();
arr.add(3,"hi");
Now I already know there will be an OutOfBoundsException. Is there another way or another object where I can do this while still keeping the order? This is because I have methods that finds elements based on their index. For e.g.:
ArrayList<String> arr = new ArrayList<String>();
arr.add("hi");
arr.add(0,"hello");
I would expect to find "hi" at index 1 instead of index 0 now.
So in summary, short of manually inserting null into the elements in-between, is there any way to satisfy these two requirements:
Insert elements into position larger than current size
Push existing elements to the right when I insert elements in the middle of the list
I've looked at Java ArrayList add item outside current size, as well as HashMap, but HashMap doesn't satisfy my second criteria. Any help would be greatly appreciated.
P.S. Performance is not really an issue right now.
UPDATE: There have been some questions on why I have these particular requirements, it is because I'm working on operational transformation, where I'm inserting a set of operations into, say, my list (a math formula). Each operation contains a string. As I insert/delete strings into my list, I will dynamically update the unapplied operations (if necessary) through the tracking of each operation that has already been applied. My current solution now is to use a subclass of ArrayList and override some of the methods. I would certainly like to know if there is a more elegant way of doing so though.
Your requirements are contradictory:
... I will need to insert new elements at specific positions.
There is a need for me to enter elements at a position larger than the current size.
These imply that positions are stable; i.e. that an element at a given position remains at that position.
I would expect to find "hi" at index 1 instead of index 0 now.
This states that positions are not stable under some circumstances.
You really need to make up your mind which alternative you need.
If you must have stable positions, use a TreeMap or HashMap. (A TreeMap allows you to iterate the keys in order, but at the cost of more expensive insertion and lookup ... for a large collection.) If necessary, use a "position" key type that allows you to "always" generate a new key that goes between any existing pair of keys.
If you don't have to have stable positions, use an ArrayList, and deal with the case where you have to insert beyond the end position using append.
I fail to see how it is sensible for positions to be stable if you insert beyond the end, and allow instability if you insert in the middle. (Besides, the latter is going to make the former unstable eventually ...)
even you can use TreeMap for maintaining order of keys.
First and foremost, I would say use Map instead of List. I guess your problem can be solved in better way if you use Map. But in any case if you really want to do this with Arraylist
ArrayList<String> a = new ArrayList<String>(); //Create empty list
a.addAll(Arrays.asList( new String[100])); // add n number of strings, actually null . here n is 100, but you will have to decide the ideal value of this, depending upon your requirement.
a.add(7,"hello");
a.add(2,"hi");
a.add(1,"hi2");
Use Vector class to solve this issue.
Vector vector = new Vector();
vector.setSize(100);
vector.set(98, "a");
When "setSize" is set to 100 then all 100 elements gets initialized with null values.
For those who are still dealing with this, you may do it like this.
Object[] array= new Object[10];
array[0]="1";
array[3]= "3";
array[2]="2";
array[7]="7";
List<Object> list= Arrays.asList(array);
But the thing is you need to identify the total size first, this should be just a comment but I do not have much reputation to do that.

Categories