I have two questions:
I am using JAVA programming language and I have found some difficulties using Arrays.
Here are some different arrays :
Object [] play1 = {0,3,6};
Object [] play2 = {0,3,6,4};
Object[][] pre = {{0,1,2},{0,3,6},{2,5,8},{6,7,8},{0,4,8},{2,4,6}};
Question 1 : Is it possible to check equals between play1 and pre using deepEquals? I also know that pre is 2D array and play1 is 1D array.
If I want to check whether play1 is equal to pre, then I might check like:
if(Arrays.deepEquals(pre, play1)){
System.out.print("true");
}else{System.out.print("false");}
Is the code correct? Even though is is possible to check equals between 1D and 2D arrays? Or do I have to use ArrayList? I am not that much familiar with ArrayList. Would appreciate if anyone explain with example.
Question 2 : However, if I want to check between play1 and play2, then also the output is false. I want to check between two arrays even though they don't have equal element but if both array consists the same element such as: {0,3,6} can be found in both play1 and play2, then the output must come true..
Thanks.
For Question2:
You can create List of objects and check as follows:
List<Object> play1List = Arrays.asList(play1);
List<Object> play2List = Arrays.asList(play2);
if(play1List.containsAll(play2List) || play2List.containsAll(play1List))
System.out.println("founD");
For Question1:
List<Object> play1List = Arrays.asList(play1);
for (int i =0 ; i< pre.length;i++){
List<Object> preList = Arrays.asList(pre[i]);
if(preList.equals(play1List)){
System.out.println("FounD"+preList);
break;
}
}
From the API docs:
Two array references are considered deeply equal if both are null, or
if they refer to arrays that contain the same number of elements and
all corresponding pairs of elements in the two arrays are deeply
equal.
From your question I understand that you are searching for a subgroup of the array.
I don't think that there's a function for that on the JDK, probably you have to develop your own function iterating the arrays.
Related
I know that there were other questions related to this topic.
But this is a little different.
Immagine that you have a program that gets every some amount of time an 2d Object (Object[][]) from the excel and it pass this to one method; this method needs to concatenate this 2d Object, so when it gets a flag that there is no more input, it passes this potentially big 2d object as a result to some other method...
1) You don't know how many excel documents will be sent , so how many 2d object will you get
2) every 2d object can have different num of columns and rows (ok this is not a big problem)
Is there a way in java to concatenate 2d objects into one? Wihout "killing" the memory.
Looking at this accepted anwer
How do you append two 2D array in java properly?
there is an append method, but each time it is called (and in this described case we don't know how many times could be called), it recreates an 2d array, and in my opition is not a best practice for this solution.
Looking at this accepted solution How to concatenate two-dimensional arrays in Java there is used an arraycopy solution, which how i understood is something similar to the previous solution... maybe a little better.
I know that would be possible to use some lists that could somehow rappresents this 2d object, or be parsed at the end in the 2d object, but:
is there any way in java (good practice) to concatenate dinamically unown number of 2d object into 1?
tnx
It look like you need ArrayList< ArrayList < Object > > instead of Object[][]. If you use ArrayList, the different num of columns and rows aren't problems at all, and you not "killing" the memory if you use addAll method. Moreover, you could use a core Java methods to concat ot ArrayLists without create your own manual methods.
For example, you can use something like this:
public List<ArrayList<Object>> concat (List<ArrayList<Object>> list1, List<ArrayList<Object>> list2) {
if(list1.size() > list2.size()) {
List<ArrayList<Object>> listBig = new ArrayList(list1); // or just list1 if we can change list1
List<ArrayList<Object>> listSmall = list2;
} else {
List<ArrayList<Object>> listBig = new ArrayList(list2); // or just list1 if we can change list1
List<ArrayList<Object>> listSmall = list1;
}
for(int i = 0; i < listSmall.size(); i++) {
listBig[i].addAll(listSmall[i]);
}
return listBig;
}
I'm trying to add 1 to an integer in a 2-dimensional ArrayList.
I'm using the set() method with the element + 1 as the second argument, but the "+ 1" isn't working. When I retrieve the element, it defines it as an object, not an integer. How do I get around this?
Code:
ArrayList<ArrayList> inventoryList = new ArrayList(
Arrays.asList(new ArrayList<String>(), new ArrayList<Integer>()));
...
(inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1));
Error:
Main.java:47: error: bad operand types for binary operator '+'
(inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1));
^
My code is at this ideone page. This code is translated from python and I'm currently debugging it so don't worry about the other errors.
ArrayList<ArrayList> inventoryList = ...
You are using the raw variant of ArrayList for your inner lists, such an ArrayList indeed contains Objects instead of Integers. You shouldn't use those raw ArrayLists and instead use generic ones:
What is a raw type and why shouldn't we use it?
Looking at your code a bit more, it seems that inventoryList is supposed to contain two lists, one that contains the items you have (as strings) and one that contains how many you have (as integers) where you can find how many you have of the item at index i in the first list by looking in the second list at that same index i.
If that is correct there are multiple ways to fix this, indeed, casting the Objects to Integers works, but then you are still using raw types, which you probably shouldn't. To fix this you should just not keep the ArrayList<String> and the ArrayList<Integer> in the same list. You could just have:
ArrayList<String> inventoryItems = ...
ArrayList<Integer> inventoryItemCounts = ...
separately (you don't need a list if it always contains exactly 2 items, a list of strings and a list of integers). However a cleaner solution would be, as was suggested in the comments by user2418306, to use a map
Map<String, Integer> inventory = ...
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html, that way each string (item) in your inventory has exactly one corresponding integer (number you have of that item) and you don't have to get that by using the "at the same index" trick.
Looking at you code a bit though, I would say that more is going wrong with the inventory. You print your inventory using:
for (int i = 0; i < inventoryList.size(); i++){
System.out.println((inventoryList.get(1)).get(i) + " : " + (inventoryList.get(0)).get(i));
}
and you iterate through it in that way in other places as well. However, if i'm not misunderstanding anything, inventoryList.size() is always going to be 2 (the inventoryList contains 2 lists, one of strings and one of integer). To get the number of distinct items (strings) in your inventory you'd have to do inventoryList.get(0).size() (or inventoryList.get(1).size() because that is going to be the same). However, things will get easier if you chose a better datatype for your inventory. I would look into the mentioned Map. Using that, you easily get the correct number using inventory.size().
You can solve your problem by casting to Integer
inventoryList.get(1).set(i, (Integer) inventoryList.get(1).get(i)+1);
Of course first take a look on comments below your question to see how to properly init your list, so you wont need explicit cast.
The way it is declared, your lists are list of Object. You need to cast the result from second get() with (Integer), like:
inventoryList.get(1).set(i, (Integer)inventoryList.get(1).get(i)+1);
Note: There are unneeded ().
I am having an Array of String of 10 elements.
Now I need to compare my value available in any of these Arrays value.
1 Option I thought of sorting the array and then binary search on the
same
But further analysis, I found the value needed to be compare is not exactly same, it contains some value , but even in that case, it should be successful.
Like Value to compare ,
String str = "Author"
String[] arrays = {"#Author","#Auth",#Au...}
str.contains(arrays..) actually but how to do the same .
you could use Dynamic Programming:
http://www.algorithmist.com/index.php/Longest_Common_Subsequence
this algorithm check the longest subsequence of string with the minimum complex
This is the algor in java:
http://introcs.cs.princeton.edu/java/96optimization/LCS.java.html
If you are only dealing with an array with only 10 elements... you don't need to worry about efficiency!
You can simply loop through the entire array, or instead of using a String[] use an List<string> and use the contains method.
I need to sort an array based on the positions held in another array.
What I have works, but it is kinda slow, is there a faster/better way to implement this?
2 Parts:
Part1
int i = mArrayName.size();
int temp = 0;
for(int j=0;j<i;j++){
temp = mArrayPosition.get(j);
mArrayName.set(temp, mArrayNameOriginal.get(j));
}
In this part, mArrayPosition is the position I would like the mArrayName to be in.
Ex.
input:
mArrayName= (one, two, three)
mArrayPosition = (2,0,1)
output:
mArrayName= (three, one two)
Part 2
int k=0;
int j=0;
do{
if(mArrayName.get(k)!=mArrayNameOriginal.get(j)){
j++;
}else{
mArrayIdNewOrder.set(k, mArrayId.get(j));
k++;
j=0;
}
}while(k < mArrayName.size());
}
In this part, mArrayName is the reordered name array, mArrayNameOriginal is the original name array.
Ex.
mArrayName = (three, one, two)
mArrayNameOriginal = (one, two, three)
Now I want to compare these two arrays, find which entries are equal and relate that to a new array that has their rowId number in it.
Ex.
input:
mArrayId = (001,002,003)
output:
mArrayIdNewOrder = (003,001,002)
So then I will have mArrayIdNewOrder id's matching up with the correct names in mArrayName.
Like I said these methods work, but is there a faster/better way to do it? I tried looking at Arrays.sort and comparators but they only seem to sort alphabetically or numerically. I saw something like I can create my own rules inside the comparator but it would probably end up being similar to what I already have.
Sorry for the confusing question. I'll try to clear up any ambiguities if needed.
The best performance read I've found is Android's Designing For Performance doc. You are violating a couple of the "Android way" style of doing things that will help you.
You are using multiple internal getters inside each loop for what looks like a simple value. Redo this by accessing the fields directly.
For extra credit, post your performance comparison results! I'd love to see em!
You could use some form of tuple, some class to hold both id and name. You'll just to have a java.util.Comparator that compares it accordingly, both elements will move together and your code will be cleaner.
This data structure might be convenient for the rest of your program... if not, just take things off it again and you're done.
If your order indexes are compact, i.e. from index 0 to size - 1, then just use an array and create the updated list afterwards? About something like
MyArray[] array = new MyArray[size];
for(int j=0;j< size;j++) {
array[ mArrayPosition.get(j) ] = mArrayName.get(j);
}
// create ArrayList from array
Is there any equivalent of String.indexOf() for arrays? If not, is there any faster way to find an array within another other than a linear search?
Regardless of the elements of your arrays, I believe this is not much different than the string search problem.
This article provides a general intro to the various known algorithms.
Rabin-Karp and KMP might be your best options.
You should be able to find Java implementations of these algorithms and adapt them to your problem.
List<Object> list = Arrays.asList(myArray);
Collections.sort(list);
int index = Collections.binarySearch(list, find);
OR
public static int indexOf(Object[][] array, Object[] find){
for (int i = 0; i < array.length(); i ++){
if (Arrays.equals(array[i], find)){
return i;
}
}
return -1;
}
OR
public static int indexOf(Object[] array, Object find){
for (int i = 0; i < array.length(); i ++){
if (array[i].equals(find)){
return i;
}
}
return -1;
}
OR
Object[] array = ...
int index = Arrays.asList(array).indexOf(find);
As far as I know, there is NO way to find an array within another without a linear search. String.indexOf uses a linear search, just inside a library.
You should write a little library called indexOf that takes two arrays, then you will have code that looks just like indexOf.
But no matter how you do it, it's a linear search under the covers.
edit:
After looking at #ahmadabolkader's answer I kind of take this back. Although it's still a linear search, it's not as simple as just "implement it" unless you are restricted to fairly small test sets/results.
The problem comes when you want to see if ...aaaaaaaaaaaaaaaaaab fits into a string of (x1000000)...aaaaaaaaab (in other words, strings that tend to match most places in the search string).
My thought was that as soon as you found a first character match you'd just check all subsequent characters one-on-one, but that performance would degrade terrifyingly when most of the characters matched most of the time. There was a rolling hash method in #a12r's answer that sounded much better if this is a real-world problem and not just an assignment.
I'm just going to vote for #a12r's answer because of those awesome Wikipedia references.
The short answer is no - there is no faster way to find an array within an array by using some existing construct in Java. Based on what you described, consider creating a HashSet of arrays instead of an array of arrays.
Normally the way you find things in collections in java is
put them in a hashmap (dictionary) and look them up by their hash.
loop through each object and test its equality
(1) won't work for you because an array object's hash won't tell you that the contents are the same. You could write some sort of wrapper that would create a hashcode based on the contents (you'd also have to make sure equals returned values consistent with that).
(2) also will require a bit of work because object equality for arrays will only test that the objects are the same. You'd need to wrap the arrays with a test of the contents.
So basically, not unless you write it yourself.
You mean you have an array which elements also are array elements? If that is the case and the elements are sorted you might be able to use binarysearch from java.util.Arrays