compare two int array in j2me? - arrays

In my app I have 2 images with same dimensions,that I would to give their RGB data and compare them.In j2me we can not use java.util.Arrays and so Arrays.equals(array1, array2) method. One way to compare them is using for loop and compare each element of two int array,but i'm looking for better way.When I search in web I found ArrayUtils class,here,that has some equals() methods,but it's method compare two arrays of objects and before compare int arrays convert them to Enumeration by arrayToEnumeration(Object array) that creates an enumeration from given object.
Finally this is my question:
Is there a better way to compare two int arrays in j2me?

Try something like this.. From the Util class
public static boolean equals(byte[] a, byte[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++)
if (a[i] != a2[i])
return false;
return true;
}

private int compareArray(String[] _array1, String[] _array2){
Vector m_length1 = new Vector();
Vector m_length2 = new Vector();
if(_array1 && _array2!=null)
{
for(int i=0; i<_array1.length; i++){
if(m_length1[i]!=null){
m_length1.add(_array1[i]);
}
}
for(int j=0; j<_array2.length; j++){
if(m_length2[j]!=null){
m_length2.add(_array2[j]);
}
}
}
if(m_length1.size()==m_length2.size()){
return 0; /*matching*/
}else{
return -1; /*no matching*/
}
}
You can modify it as int array or you can compare byte of each value.
For instance;
byte sample[] = {0,0,0,0};
sample= yourValue.getBytes();

Convert byte array to String:
public boolean equals(byte[] b1, byte[] b2){
String strB1 = new String(b1);
String strB2 = new String(b2);
if(strB1.equals(strB2)){
return true;
}
else{
return false;
}
}

Related

One-dimensional array comparison within a two-dimensional array in C

I want to compare two arrays of different sizes in a particular function. I wrote a function like this:
static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])
{
static int index=0;
static bool isequal = false;
for(int i = 0 ; i<count_current; i++ )
{
for(int j = 0; j<6 ; j++ )
{
if (array_1[j] == array_2[i][j])
{
index++ ;
}
else
{
index=0;
return false;
}
}
if (index == 6)
{
index=0;
isequal = true;
i = count_current + 2;
}
else
{
index=0;
isequal = false;
}
}
return isequal;
}
Define some variables;
static bool matching_array;
static uint8_t one_dimensional_array[6];
static uint8_t two_dimensional_array[10][6];
Then I use the function like this;
matching_array= compare_arrays( one_dimensional_array, two_dimensional_array);
But I got an error like this;
..\..\..\main.c(857): error: #98: an array may not have elements of this type
Do you have any suggestion ?
static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])
The second dimension must be present - otherwise compiler can't decide how much position to stride through when someone writes like array_2[y][x].
static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][6])
Actually 2d array decays into pointer to it's first element - which is of type uint8_t (*)[6]. And the single dim array will similarly decay here into uint8_t*. Remember that the first dimension of passed array is not considered by compiler.Arrays declarations must have all, except the first, sizes defined.

If the input is a1bc2def3 output should be abcbcdefdef

If the given input is a1bc2def3 then output should be abcbcdefdefdef
Whenever the number comes then we should repeat previous substring that many number of times.
Please provide the algorithm or code to accomplish this.
Here's another approach that doesn't rely on regex.
public String splitRepeat(String str)
{
StringBuilder out = new StringBuilder();
boolean number = false;
for(int i=0,j=0,k=0; i<=str.length(); i++)
{
if(i==str.length() || Character.isDigit(str.charAt(i)) != number)
{
if(number)
{
for(int r = Integer.parseInt(str.substring(j, i)); r>0; r--)
{
out.append(str.substring(k, j));
}
}
else
{
k=j;
}
j=i;
number = !number;
}
}
return out.toString();
}
My suggestion would be:
Try using regex so you can get an array of numbers and characters,
then convert the number Parsable elements of the array into an integer,
after that loop with the index of the arrays to append n-times the characters of the array
then print the final result
Example:
public static void main(String[] args) {
String stringToProcess = "a1bc2def3";
String[] regexSplitted = stringToProcess.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
StringBuilder sb = new StringBuilder();
String appender = "";
for (int i = 0; i < regexSplitted.length; i++) {
try {
int kilo = Integer.parseInt(regexSplitted[i]);
for (int j = 0; j < kilo; j++) {
sb.append(appender).append(regexSplitted[i - 1]);
appender = "-";
}
} catch (NumberFormatException e) {
}
}
System.out.println(sb.toString());
}
this will print
a-bc-bc-def-def-def
that is pretty much what you are looking for.

sorting strings in an array alphabetically

I came up with this code to try and sort out an array of strings (I don't want to use ints or imports as I'm trying to understand iteration loops), but it only rearranges the first two strings. Can anyone point out my mistakes?
public class Alpha8 {
public static void main(String[] args) {
String[] Names = {"O","B","K","S","D","M","N","A"};
String temp = null;
for(int i=0;i<Names.length;i++){
for(int j = i+1;j<Names.length;j++) {
if(Names[j].compareTo(Names[i])<0)
temp = Names[i];
Names[i] = Names[j];
Names[j] = temp;
for( i = 0;i<Names.length;i++){
System.out.println(Names[i]);
}
}
}
}
}
You have made two mistakes:
You are printing the array inside your 'swap' code. You should only print the array once the sorting is complete.
You only iterate through the array once. For a bubble sort (which is what you are implementing) you need to keep iterating through until no swaps occur.
The method should look something like:
boolean hasSwapped;
do {
hasSwapped = false;
for (int i = 1; i < names.size(); i++) {
if (names[i-1].compareTo(names[i]) > 0) {
swap(names[i-1], names[i]);
hasSwapped = true;
}
}
} while (hasSwapped);

File not loading properly in C

I'm trying to compare a string using a pointer to the array and the destination as defined.
string destination;
int flightcompare(Flights FDA[], String destination)
{
int j=0;
Flights founddestination[10];
for (int i=0;i<MAXARRAYSIZE;i++)
{
(strcmp(Flight *FDA[i]->destination,destination)==0);
founddestination[j]= FDA[i];
j++;
}
return 1;
}
I am not sure about the programming language but I am assuming it is one with stringas a data type.
In your code, there's a Semicolon at the end of
strcmp(Flight *FDA[i]->destination,destination)==0);
which makes use of strcmpredundant.
Remove that Semicolon.
Plus You don't need to pass Flight*to strcmp
So, With these modifications, Function should look like:
int flightcompare(Flights FDA[], String destination)
{
int j=0;
Flights founddestination[10];
for (int i=0;i<MAXARRAYSIZE;i++)
{
if(strcmp(FDA[i]->destination,destination)==0)
{
founddestination[j]= FDA[i];
j++;
if(j >= 10)
{
break; // Stop Looping as Array is full
}
}
}
return j; // Return Count of the Flights found.
}
your strcmp line doesn't make any sense, cause you're not checking the boolean value which is the result of your comparison.
In general, it should be put in an if statement.
There's another problem though since there's no need to compare string objects with strcmp.
You can just compare them with operator ==.
if (FDA[i].destination == destination) {
// they're equal -> do something
} else {
// they're not equal -> do something else
}
That's assuming Flights type has a 'destination' public member.
Moreover, why put them in the founddestination array if you're not using it? and what is the purpose of returning an int?
If you want to know if there's any mismatching destination you can return a boolean.
If you want to return the number of equal destinations / non equal destinations you can just count them in an int.
Assuming you aim for the boolean solution i'd write:
bool flightcompare(Flights FDA[], String destination) {
for (int i = 0; i < MAXARRAYSIZE; ++i) {
if (FDA[i].destination != destination) {
return false;
}
}
return true;
}
If you want to return the amount of matching flights i'd write:
int flightcompare(Flights FDA[], String destination) {
int count = 0;
for (int i = 0; i < MAXARRAYSIZE; ++i) {
if (FDA[i].destination == destination) {
++count;
}
}
return count;
}

Processing 2.0 array sort

I am getting this error while trying to sort an array of objects:
the method sort (byte[]) in the type PApplet is not applicable for the arguments (sketch_124453.Word[])
To support sort, I saw that one has to implement Comparable, hence my class code below.
But its not working so I wonder if sorting object arrays is possible with Processing 2.0+? Is this solution approach that I have done specific only to Processing 1.0?
class Word implements Comparable {
String s;
int n=0;
Word(String theWord) {
s = theWord;
n = 1;
}
//if we want to sort based on the n value of Word object:
int compareTo(Object o)
{
Word other=(Word)o;
if(other.n>n) return -1;
if(other.n==n) return 0;
return 1;
}
int compareTo(Word o)
{
if(o.n>n) return -1;
if(o.n ==n) return 0;
return 1;
}
}
I believe that to use Comparable you need to use Java's Arrays.sort() instead of Processing's one. Also AFAIK you don't need two compareTo() functions. As a bonus I made a toString function so println() can handle the object properly.
check it out;
import java.util.Arrays;
Word[] words = new Word[3];
void setup() {
words[0] = new Word("three", 3);
words[1] = new Word("two", 2);
words[2] = new Word("one", 1);
println(words);
Arrays.sort(words);
println("sorted:");
println(words);
}
class Word implements Comparable {
String s;
int n;
Word(String theWord, int _n) {
s = theWord;
n = _n ;
}
//if we want to sort based on the n value of Word object:
int compareTo(Object o)
{
Word other=(Word)o;
if (other.n > n) return -1;
if (other.n == n) return 0;
return 1;
}
String toString() {
return s + " - n = " + n;
}
}
As you need to import java.util.Arrays this won't work in processingjs.

Resources