getting error StringIndexOutOfBoundsException: String index out of range [closed] - java

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying to build a matrix with row and column having values such as "aaa" for aligning purposes. but when I run it I get an error. below is my code
public class compute_matrix {
static String seq1="aaa";
static String seq2="aaa";
static int[][] matrix;
static int max_row;
static int max_col;
private static int match_reward=1;
private static int mismatch_penalty= -1;
private static int gap_cost= -1;
private static boolean case_sensitive;
private static boolean isCaseSensitive() {
return case_sensitive;
}
private static int max(int ins, int sub, int del, int i) {
if (ins > sub) {
if (ins > del) {
return ins > i? ins : i;
} else {
return del > i ?del : i;
}
} else if (sub > del) {
return sub> i ? sub : i;
} else {
return del > i ? del : i;
}
}
protected char sequence[];
public static void main(String args[]){
int r, c, rows, cols, ins, sub, del, max_score;
rows = seq1.length()+1;
cols = seq2.length()+1;
matrix = new int [rows][cols];
// initiate first row
for (c = 0; c < cols; c++)
matrix[0][c] = 0;
// keep track of the maximum score
max_row = max_col = max_score = 0;
// calculates the similarity matrix (row-wise)
for (r = 1; r < rows; r++)
{
// initiate first column
matrix[r][0] = 0;
for (c = 1; c < cols; c++)
{
sub = matrix[r-1][c-1] + scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
ins = matrix[r][c-1] + scoreInsertion(seq2.charAt(c));
del = matrix[r-1][c] + scoreDeletion(seq1.charAt(r));
// choose the greatest
matrix[r][c] = max (ins, sub, del, 0);
if (matrix[r][c] > max_score)
{
// keep track of the maximum score
max_score = matrix[r][c];
max_row = r; max_col = c;
}
}
}
}
private static int scoreSubstitution(char a, char b) {
if (isCaseSensitive())
if (a == b)
return match_reward;
else
return mismatch_penalty;
else
if (Character.toLowerCase(a) == Character.toLowerCase(b))
return match_reward;
else
return mismatch_penalty;
}
private static int scoreInsertion(char a) {
return gap_cost;
}
private static int scoreDeletion(char a) {
return gap_cost;
}
public char charAt (int pos)
{
// convert from one-based to zero-based index
return sequence[pos-1];
}
}
and my error is displaying this
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:695)
at compute_matrix.main(compute_matrix.java:67)
Java Result: 1

rows = seq1.length()+1;
cols = seq2.length()+1;
matrix = new int [rows][cols];
and then later:
for (c = 1; c < cols; c++)
{
//when c == cols-1, it is also `seq2.length()`
//the access to seq2.charAt(c) will cause this exception then.
sub = matrix[r-1][c-1] + scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
ins = matrix[r][c-1] + scoreInsertion(seq2.charAt(c));
del = matrix[r-1][c] + scoreDeletion(seq1.charAt(r));
In the above loop, when c == cols-1, it is also seq2.length(), the access to seq2.charAt(c) will cause this exception then.
You initialize the number of rows and cols to length() + 1, while you later iterate from 0 to length (inclusive), while the string contain only length() chars - from 0 to n exclusive.
If you are a C programmer in your past - I assume you are expecting a \0 terminator at the end of the string. In java you don't have those - since String is an object - you can hold a field to indicate its exact length. Meaning the last char in the string, is actually the last character there.

in line 60 of your code
sub = matrix[r-1][c-1] + scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
max value for r is 4 so when you look up for seq.charAt(3) there is nothingso it shows index out of bound

I refactored your code into more canonical java.
The things I've changed:
The class is now called SimilarityMatrix, a more appropriate, self documenting name
variable declarations now happen where they get used as opposed to at the top of main
The work is now done in an instance of the class rather than the main method
I used the built in Math.max(int, int) instead of rolling my own
I removed a lot of unnecessary nested if statements. Java's short circuit evaluation helps here
Since both r and c as well as r+1 and c+1 are used frequently in your calculation loop, I track both
I removed many of the dependencies on static state (made many things instance variables)
Static state that remains is all final now (I made them constants)
Used more java-y variable names (java people really like their camel case)
public class SimilarityMatrix
{
public static final int matchReward = 1;
public static final int mismatchPenalty = -1;
public static final int gapCost = -1;
private int[][] matrix;
private int maxRow = 0;
private int maxCol = 0;
private boolean caseSensitive = false;
SimilarityMatrix(String s1, String s2, boolean dontIgnoreCase)
{
this(s1, s2);
caseSensitive = dontIgnoreCase;
}
SimilarityMatrix(String s1, String s2)
{
int rows = s1.length() + 1;
int cols = s2.length() + 1;
matrix = new int[rows][cols];
int max_score = 0;
for (int x = 0; x < cols; x++)
{
matrix[0][x] = 0;
matrix[x][0] = 0;
}
for (int r = 0, rp1 = 1; rp1 < rows; ++r, ++rp1)
{
for (int c = 0, cp1 = 1; cp1 < rows; ++c, ++cp1)
{
int sub = matrix[r][c] + scoreSubstitution(s1.charAt(r), s2.charAt(c));
int ins = matrix[rp1][c] + scoreInsertion(s2.charAt(c));
int del = matrix[r][cp1] + scoreDeletion(s1.charAt(r));
// choose the greatest
matrix[rp1][cp1] = Math.max(Math.max(ins, sub), Math.max(del, 0));
if (matrix[rp1][cp1] > max_score)
{
// keep track of the maximum score
max_score = matrix[rp1][cp1];
maxRow = rp1;
maxCol = cp1;
}
}
}
}
public static void main(String args[])
{
SimilarityMatrix me = new SimilarityMatrix("aaa", "aaa");
System.out.println(me.getMaxRow() + " " + me.getMaxCol());
}
private int scoreSubstitution(char a, char b)
{
if ((a == b && caseSensitive) || Character.toLowerCase(a) != Character.toLowerCase(b))
return matchReward;
else
return mismatchPenalty;
}
public int getMaxRow()
{
return maxRow;
}
public int getMaxCol()
{
return maxCol;
}
private int scoreInsertion(char a)
{
return gapCost;
}
private int scoreDeletion(char a)
{
return gapCost;
}
}

Related

Pattern in a matrix with dot and hash

Find patterns in a given matrix.
I have a matrix with values '.' and '#'
Now I want to find count with the given pattern in the matrix :
a)
##
b)
#
#
For the above pattern, the cells if exist surrounding them should be '.'
I am able to create successful logic for it:
static int getMatches(String[] B, int m, int n) {
int count = 0;
boolean[][] flag = new boolean[m][n];
for (int i = 0; i < m; i++) {
String S = B[i];
for (int j = 0; j < n; j++) {
char c = S.charAt(j);
boolean valid = true;
if (c == '#' && flag[i][j] == false) {
if (j + 1 < n && S.charAt(j + 1) == '#' && flag[i][j + 1] == false) {
int[][] adj = { { 0, -1 }, { 0, 2 }, { -1, 0 }, { -1, 1 }, { 1, 0 }, { 1, 1 } };
valid = isValid(adj, i, j, m, n, B);
} else {
valid = false;
}
} else {
valid = false;
}
if (c == '#' && !valid && flag[i][j] == false) {
if (i + 1 < m && S.charAt(i + 1) == '#' && flag[i + 1][i] == false) {
int[][] adj = { { 0, -1 }, { 1, -1 }, { 1, 1 }, { 0, 1 }, { -1, 0 }, { 2, 0 } };
valid = isValid(adj, i, j, m, n, B);
} else {
valid = false;
}
}
flag[i][j] = true;
if (valid) {
count++;
}
}
}
return count;
}
static boolean isValid(int[][] adj, int i, int j, int m, int n, String[] B) {
for (int a = 0; a < adj.length; a++) {
int i1 = i + adj[a][0];
int j1 = j + adj[a][1];
if (i1 >= 0 && i1 < m) {
if (j1 >= 0 && j1 < n) {
char d = B[i1].charAt(j1);
if (d == '#') {
return false;
}
}
}
}
return true;
}
Now I want to change the pattern to 3 cells like this:
a)
###
b)
#
#
#
c)
##
#
d)
##
#
e)
#
##
f)
#
##
How to build logic for this? Is their a way to extend my above code or is there a better approach to solve this problem.
As far as I understand, the problem can be simplified to finding the adjacent # fields, and their area must match a certain number.
So I have created the following code:
public static int getMatches(String[] matrix, int m, int n, int patternArea) {
int count = 0;
for (int i = 0; i < m; i++) {
String row = matrix[i];
for (int j = 0; j < n; j++) {
char item = row.charAt(j);
if (item == '#' && countArea(matrix, i, j) + 1 == patternArea) {
count++;
}
}
}
return count;
}
private static int countArea( String[] matrix, final int i, final int j ) {
int area = 0;
setVisited(matrix, i,j);
if (matrix[Math.max(i-1, 0)].charAt(j) == '#') {
area += countArea(matrix, i-1, j) + 1;
}
if (matrix[Math.min(i+1, matrix.length-1)].charAt(j) == '#') {
area += countArea(matrix, i+1, j) + 1;
}
if (matrix[i].charAt(Math.max(j-1, 0)) == '#') {
area += countArea(matrix, i, j-1) + 1;
}
if (matrix[i].charAt(Math.min(j+1, matrix[0].length()-1)) == '#') {
area += countArea(matrix, i, j+1) + 1;
}
return area;
}
private static void setVisited( final String[] matrix, final int i, final int j ) {
char[] rowChars = matrix[i].toCharArray();
rowChars[j] = '0';
matrix[i] = String.valueOf(rowChars);
}
Explanation:
Find at least 1 # field
From that position, recursively find any adjacent # fields - countArea method
Always mark already visited fields - I chose 0, but any other char will do other than . and #
Finally, if the area is matching a given number (patternArea), increase count variable
This is working only, if you include all of the patterns for a certain number. Given your example, you did just that. In case you really want to do exact pattern matching, then this approach will not be useful for your case.
Shared solution should work with any matrix of any symbols and number of rows/columns
Let's assume next parameters that we'll have as our input:
Matrix is represented as two-dimensional array - matrix[r][c], where r - number of rows in matrix, c - number of columns in matrix
Matrix has all its cells filled with some symbols
Pattern is represented as two-dimensional array - arr[k][m], where k - number of rows in pattern, m - number of columns in pattern
Algorithm will iterate each column of each row in matrix
Pattern can potentially be present in cell[x][y] only if next conditions are met:
x + k <= r (there are enough of matrix rows below this one (including current row) to include number of pattern rows)
y + m <= c (there are enough of matrix columns right of this one (including current column) to include number of pattern columns)
Comparison of pattern to current matrix cells says that they are equal. Comparison in done based on next conditions:
if pattern character is null - continue comparison
if pattern character is equal to matrix character - continue comparison
if pattern character is not equal to matrix character - end comparison, pattern is not located in this cell
if all pattern symbols checked - pattern is found in given matrix cell
Result of comparison in represented in 0-based index pairs
Let's get to the code.
We will have next classes:
Pattern Rule - represents pattern to be found. please specify nulls manually to make actually 2-dimensional array of equal row/col count
private static final class PatternRule {
private final Character[][] pattern;
private final int rowCount;
private final int colCount;
private PatternRule(Character[][] pattern) {
this.pattern = pattern;
this.rowCount = pattern.length;
this.colCount = pattern[0].length;
}
public int getRowCount() {
return rowCount;
}
public int getColCount() {
return colCount;
}
public Character getCharacterAt(int row, int column) {
return pattern[row][column];
}
}
Found Pattern Location - class to represent found cells for pattern locations
private static final class FoundPatternLocation {
private final int rowIndex;
private final int colIndex;
private FoundPatternLocation(int rowIndex, int colIndex) {
this.rowIndex = rowIndex;
this.colIndex = colIndex;
}
#Override
public String toString() {
return "[" + rowIndex + "][" + colIndex + "]";
}
}
Matrix - class that creates matrix and performs search of pattern locations
private static final class Matrix {
private final Character[][] matrix;
private final int rowCount;
private final int colCount;
private Matrix(int rowCount, int colCount, Character[][] matrix) {
this.rowCount = rowCount;
this.colCount = colCount;
this.matrix = matrix;
}
public static Matrix createMatrix(int rowCount, int colCount, char[] matrixSymbols) {
Character[][] newMatrix = new Character[colCount][rowCount];
Random rand = new Random();
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
for (int colIndex = 0; colIndex < colCount; colIndex++) {
// select random char from allowed symbols
newMatrix[rowIndex][colIndex] = matrixSymbols[rand.nextInt(matrixSymbols.length)];
}
}
return new Matrix(rowCount, colCount, newMatrix);
}
public void printMatrix() {
System.out.println("---- Matrix -----");
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
List<String> rowValues = new ArrayList<>();
for (int colIndex = 0; colIndex < colCount; colIndex++) {
rowValues.add(String.valueOf(matrix[rowIndex][colIndex]));
}
String printableRowString = String.join(" ", rowValues);
System.out.println(printableRowString);
}
System.out.println("-----------------");
}
public List<FoundPatternLocation> findPatternLocations(PatternRule patternRule) {
List<FoundPatternLocation> actualPatternLocation = new ArrayList<>();
for (int rowNum = 0; rowNum < this.rowCount; rowNum++) {
for (int colNum = 0; colNum < this.colCount; colNum++) {
if (isPatternFittingToCurrentCell(rowNum, colNum, patternRule)) { // can pattern be located in this cell?
if (isPatternLocatedInCell(rowNum, colNum, patternRule)) { // is pattern actually located in this cell?
actualPatternLocation.add(new FoundPatternLocation(rowNum, colNum));
}
}
}
}
return actualPatternLocation;
}
private boolean isPatternFittingToCurrentCell(int matrixRowIndex, int matrixColIndex, PatternRule patternRule) {
int patternRowsCount = patternRule.getRowCount();
int patternColumnsCount = patternRule.getColCount();
int availableMatrixRowsForPattern = rowCount - matrixRowIndex;
int availableMatrixColsForPattern = colCount - matrixColIndex;
return patternRowsCount <= availableMatrixRowsForPattern && patternColumnsCount <= availableMatrixColsForPattern;
}
private boolean isPatternLocatedInCell(int rowNum, int colNum, PatternRule patternRule) {
int patternRowsCount = patternRule.getRowCount();
int patternColumnsCount = patternRule.getColCount();
for (int currentPatternRowIndex = 0; currentPatternRowIndex < patternRowsCount; currentPatternRowIndex++) {
for (int currentPatternColIndex = 0; currentPatternColIndex < patternColumnsCount; currentPatternColIndex++) {
int currentMatrixRowIndex = currentPatternRowIndex + rowNum;
int currentMatrixColIndex = currentPatternColIndex + colNum;
Character currentMatrixCharacter = matrix[currentMatrixRowIndex][currentMatrixColIndex];
Character currentPatternCharacter = patternRule.getCharacterAt(currentPatternRowIndex, currentPatternColIndex);
if (currentPatternCharacter == null) {
continue;
}
if (Objects.equals(currentMatrixCharacter, currentPatternCharacter)) {
continue;
}
return false;
}
}
return true;
}
}
Class to perform execution
public static void main(String[] args) {
int rowCount = 5;
int colCount = 5;
char[] matrixSymbols = new char[] {'✓', 'X'};
Matrix matrix = Matrix.createMatrix(rowCount, colCount, matrixSymbols);
matrix.printMatrix();
PatternRule patternRuleA = createPatternRuleA();
PatternRule patternRuleB = createPatternRuleB();
PatternRule patternRuleC = createPatternRuleC();
List<FoundPatternLocation> patternLocationsA = matrix.findPatternLocations(patternRuleA);
printPatternLocations(patternLocationsA, "Pattern A");
List<FoundPatternLocation> patternLocationsB = matrix.findPatternLocations(patternRuleB);
printPatternLocations(patternLocationsB, "Pattern B");
List<FoundPatternLocation> patternLocationsC = matrix.findPatternLocations(patternRuleC);
printPatternLocations(patternLocationsC, "Pattern C");
}
private static void printPatternLocations(List<FoundPatternLocation> patternLocations, String patternName) {
System.out.println("---- " + patternName + " ----");
String string = patternLocations.stream().map(FoundPatternLocation::toString).collect(Collectors.joining("; "));
System.out.println(string);
System.out.println("--------");
}
private static PatternRule createPatternRuleA() {
Character[][] chars = {
{'✓', '✓', '✓'}
};
return new PatternRule(chars);
}
private static PatternRule createPatternRuleB() {
Character[][] chars = {
{'✓'},
{'✓'},
{'✓'}
};
return new PatternRule(chars);
}
private static PatternRule createPatternRuleC() {
Character[][] chars = {
{'✓', '✓'},
{'✓', null}
};
return new PatternRule(chars);
}
Example of execution:
---- Matrix -----
✓ ✓ X ✓ X
✓ ✓ ✓ ✓ ✓
X X X X ✓
X ✓ X X ✓
X X ✓ ✓ ✓
-----------------
---- Pattern A ----
[1][0]; [1][1]; [1][2]; [4][2]
--------
---- Pattern B ----
[1][4]; [2][4]
--------
---- Pattern C ----
[0][0]
--------

What's wrong with my code in Java?

I have written a code that is supposed to count the following,
here some examples:
x(2) = 1 * (1+2) = 3
x(4) = 1 * (1+2) * (1+2+3) * (1+2+3+4) = 180
x(5) = 1 * (1+2) * (1+2+3) * (1+2+3+4) * (1+2+3+4+5) = 2700
Edit:
Thx a lot so far to everyone!
I didn't expect to get help that fast and precise, really nice :D
Just modified my code now and I no longer get 0 as result (no matter what i typed) which is very good.
But I think there is another mistake left somewhere,
let's say I type into console 2, I will get 2 as result.
If I type 4, I get 24 as result. For 5 I get 120 and 6 I get 720.
From this I could realize one thing.
If i divide let's say 720 by 6, I get 120 which is the previous result (of 5).
And if I take result of 5 which is 120 and divide it by 4, I get 24.
public class CounIt
{
public static int i;
public static int j;
public static int b;
public static int c;
public static int a (int k)
{
j = 0;
for (i = 1; i <= k; ++i)
j = j + 1;
return j;
}
public static int x (int k)
{
b = 1;
for (c = 1; c <= k; ++c)
b = b * a(c);
return b;
}
public static void main (String[] args)
{
int k = Integer.parseInt(args[0]);
System.out.println(x(k));
}
}
Two problems:
b remains 0 in the function x(int), so this function always returns 0. Shouldn't it be initialised to 1?
The function a(int) returns the input parameter. Didn't you mean to return j?
Also, having single character function names makes your program tricky to read.
Because you initialized b to zero every multiplication of b will give you 0
b = 0; // Set b to zero
for (c = 1; c <= k; ++c)
b = b * a(c); // b will stay to 0 because b = 0 * a(c);
Modify your code to
b = 1; // <---- Here the modification
for (c = 1; c <= k; ++c)
b = b * a(c);
To be sure that your code works as expected you should unit test your functions. Take a look at TDD metodology, it will save you a lot of time for bigger projects. And here a link to a nice tutorial to unit tests.
Try this:
public class CountIt
{
public static int i;
public static int j;
public static int b;
public static int c;
public static int a (int k)
{
j = 0;
for (i = 1; i < k; ++i)
j = j + 1;
return j;
}
public static int x (int k)
{
b = 1;
for (c = 1; c <= k; ++c)
b = b * a(c);
return b;
}
public static void main (String[] args)
{
int k = Integer.parseInt(args[0]);
System.out.println(x(k));
}
}
This function is very suitable for recursion:
public class SumPyramid {
public static void main (String[] args) {
System.out.println(sumPyramid(5));
}
public static int sumPyramid(int height) {
if (height == 1) {
return 1;
}
else
return height + sumPyramid(height - 1);
}
}
}
Both functions a and x have errors. Modify functions as below.
Logically its working
public static int a (int k)
{
j = 0;
for (i = 1; i <= k; ++i) // changed consition to less than or equal to
j = j + 1;
return j; // returns j
}
public static int x (int k)
{
b = 1; // changed initial value to 1
for (c = 1; c <= k; ++c)
b = b * a(c);
return b;
}

Sorting a time intervals

I am giving a time interval in the form of two arrays.
A[0]= 2 B[0]=3
A[1]= 9 B[1]=11
A[2] = 5 B[2]=6
A[3] = 3 B[3]=10
I want to sort the interval on the basics of starting time i.e.
(2,3) , (3,10) ,(5,6) ,(9,11)
Does i have to make a structure of this. or it can be done straight.
Try:
private static class StartEnd implements Comparable<StartEnd> {
private final int start;
private final int end;
// + constructor + getters
#Override
public int compareTo(StartEnd other) {
return start - other.getStart();
}
}
public void sort(int[] starts, int[] ends) {
StartEnd[] ses = new StartEnd[starts.length];
for(int i = 0 ; i < starts.length ; ++i) {
ses[i] = new StartEnd(starts[i], ends[i]);
}
Arrays.sort(sis);
// re-insert
for(int i = 0 ; i < ses.length ; ++i) {
starts[i] = ses[i].getStart;
ends[i] = ses[i].getEnd();
}
}
It can be done straight, since you dont show what have you tried so far I just give you the algorithm:
for j = 1 to n
for i = i+1 to n
if(A[i]>A[j]){
swap(A[i],A[j])
swap(B[i],B[j])
}
you can easily convert it to java code.
this algorithm is buble sort if you want better algorithm use this wiki link to improve your time.
As DwB want here is merge sort full java code that do what you want. I got merge sort algorithm from here and modify to satisfy your need. also you could see the working version on Ideone
Merge Sort:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
private int[] A;
private int[] B;
private int[] helperA;
private int[] helperB;
private int length;
public static void main (String[] args){
int[] As = {2,9,5,3};
int[] Bs = {3,11,6,10};
new Ideone().sort(As,Bs);
}
public void sort(int[] As , int[] Bs) {
A = As;
B = Bs;
length = A.length;
this.helperA = new int[length];
this.helperB = new int[length];
mergesort(0, length - 1);
for(int i = 0 ; i<length ; i++)
System.out.println("(" + A[i] + "," + B[i]+ ")");
}
private void mergesort(int low, int high) {
// check if low issmaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
private void merge(int low, int middle, int high) {
// Copy both parts into the helper array
for (int i = low; i <= high; i++) {
helperA[i] = A[i];
helperB[i] = B[i];
}
int i = low;
int j = middle + 1;
int k = low;
// Copy the smallest values from either the left or the right side back
// to the original array
while (i <= middle && j <= high) {
if (helperA[i] <= helperA[j]) {
A[k] = helperA[i];
B[k] = helperB[i];
i++;
} else {
A[k] = helperA[j];
B[k] = helperB[j];
j++;
}
k++;
}
// Copy the rest of the left side of the array into the target array
while (i <= middle) {
A[k] = helperA[i];
B[k] = helperB[i];
k++;
i++;
}
}
}
Step 1: Java is an object oriented language; learn to use objects.
Possible class for the time interval
public class TimeInterval implements Comparable<TimeInterval>
{
private int end;
private int start;
public TimeInterval(
final int end,
final int start)
{
this.end = end;
this.start = start;
}
public int getEnd()
{
return end;
}
public int getStart()
{
return start;
}
public int comareTo(final TimeInterval other)
{
if (other == null)
{
return -1; // this will put the null value objects at the end.
}
return start - other.start;
}
}
The classical Javanese "object oriented" approach for this is to use a dedicated class storing a pair of values (int values, in this case), and sort them, as already pointed out in most of the other answers. However, I'd recommend to not make this class Comparable. Instead, a Comparator could be used, which would make it much easier to introduce new sorting orders. Particularly, there could be Comparator implementations for sorting in ascending/descending order, based on the first/second value, respectively. Only then, object orientation plays out its advantages, compensating the "disadvantage" of having to create such a pair of int values as a "dummy data structure" in the first place...
However, I wanted to try to find a solution for the original question as well, namely, sorting two arrays "in sync". Despite the task of sorting seemingly being trivial, one can dedicate a lot of work to doing it right (see Chapter 3 of TAOCP). A bubble sort is simple but inefficient even for medium-sized arrays. Implementing a quick- or merge sort can be fiddly when trying to get the indices right. However, one solution can be obtained by simply taking the existing sort method from java.lang.Arrays, and factoring out the most elementary building block: The swap function:
public class ArraySort
{
public static void main(String[] args)
{
final int A[] = new int[4];
final int B[] = new int[4];
A[0] = 2; B[0] = 3;
A[1] = 9; B[1] = 11;
A[2] = 5; B[2] = 6;
A[3] = 3; B[3] = 10;
Swapper swapper = new Swapper()
{
#Override
public void swap(int array[], int i0, int i1)
{
ArraySort.swap(A, i0, i1);
ArraySort.swap(B, i0, i1);
}
};
sort(A, 0, A.length, swapper);
for (int i=0; i<A.length; i++)
{
System.out.println("("+A[i]+","+B[i]+")");
}
}
interface Swapper
{
void swap(int array[], int i0, int i1);
}
public static void swap(int array[], int i0, int i1)
{
int t = array[i0];
array[i0] = array[i1];
array[i1] = t;
}
// The following methods are copied from java.util.Arrays:
public static void sort(int x[], int off, int len, Swapper swapper)
{
if (len < 7)
{
for (int i = off; i < len + off; i++)
{
for (int j = i; j > off && x[j - 1] > x[j]; j--)
{
swapper.swap(x, j, j - 1);
}
}
return;
}
int m = off + (len >> 1);
if (len > 7)
{
int l = off;
int n = off + len - 1;
if (len > 40)
{
int s = len / 8;
l = med3(x, l, l + s, l + 2 * s);
m = med3(x, m - s, m, m + s);
n = med3(x, n - 2 * s, n - s, n);
}
m = med3(x, l, m, n);
}
int v = x[m];
int a = off, b = a, c = off + len - 1, d = c;
while (true)
{
while (b <= c && x[b] <= v)
{
if (x[b] == v)
{
swapper.swap(x, a++, b);
}
b++;
}
while (c >= b && x[c] >= v)
{
if (x[c] == v)
{
swapper.swap(x, c, d--);
}
c--;
}
if (b > c)
{
break;
}
swapper.swap(x, b++, c--);
}
int s, n = off + len;
s = Math.min(a - off, b - a);
vecswap(x, off, b - s, s, swapper);
s = Math.min(d - c, n - d - 1);
vecswap(x, b, n - s, s, swapper);
if ((s = b - a) > 1)
{
sort(x, off, s, swapper);
}
if ((s = d - c) > 1)
{
sort(x, n - s, s, swapper);
}
}
private static void vecswap(int x[], int a, int b, int n, Swapper swapper)
{
for (int i = 0; i < n; i++, a++, b++)
{
swapper.swap(x, a, b);
}
}
private static int med3(int x[], int a, int b, int c)
{
return (x[a] < x[b] ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
: (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
}
}
Notes
This is not a solution that I would recommend. It's just an attempt to answer the question
or it can be done straight. [sic!]
And the answer is: Yes, it is possible, although the solutions that are introducing some sort of an IntPair are more idiomatic.
Apart from that, it would probably be more efficient to "inline" the Swapper#swap calls to directly swap elements of two arrays that are stored in instance variables, or passed as method parameters. However, I liked the genericity of such a Swapper interface. Additionally, it would be nice to generalize this even further, by passing in something like a
interface IntArrayEntryComparator {
int compare(int array[], int i0, int i1);
}
But the latter would go beyond what I wanted to test/demonstrate with this class.
instead having two arrays, create object which holds your intervals
class Interval implements Comparable<Interval> {
private Long start,completed
public Interval(Long start, Long completed) {
this.start = start;
this.completed = completed;
}
#Override
public int compareTo(Interval o) {
return start.compareTo(o.start);
}
//getters and setters ommited
}
then, all what you need to do is implement compareTo method and put all your data in some collection ie List<Interval> intervals
and used Collections.sort(intervals) to sort them
EDIT
Example:
originally you have:
A[0]= 2 B[0]=3,
A[1]= 9 B[1]=11
A[2] = 5 B[2]=6
A[3] = 3 B[3]=10`
lets replace this by:
List<Interval> intervals = new ArrayList<>();
intervals.add(new Interval(2L,3L));
intervals.add(new Interval(9L,11L));
intervals.add(new Interval(5L,6L));
intervals.add(new Interval(3L,10L));
//NOTE L is added at the end variable as interval uses Long, if you change it to integer you dont need to add it;
And now all what you need to do is sort
Collection.sort(intervals);

How to define an exponent as the index position of a string

I am trying to make an int method that converts a binary number into a base 10 number. I think my loop is structured correctly, but I cant figure out how to relate index position to an exponent. Basically if there is a '1' in the string, i want to return it as 2 to the power of whatever the index position of that char is. Also, this would require me to inverse the index (so that the 0 position is the rightmost char of the string. Here is what I have so far:
public static int BinaryToNumber(String numberInput)
{
int len = numberInput.length();
for(int i=len-1; i<len; i--)
{
if(i == '1');
{
return n;
}
}
return 0;
}
Thank you in advance!
I would prefer the Java built-in routines when possible - as I said in my comment Integer.parseInt(numberInput, 2);. By convention, Java method names begin with a lower case letter. Finally, you can fix your code (and I added a small test harness) with something like,
public static int binaryToNumber(String numberInput) {
if (numberInput == null) {
return 0;
}
int ret = 0;
char[] ni = numberInput.trim().toCharArray();
for (int i = 0; i < ni.length; i++) {
if (ni[i] == '1') {
// This is 2 ^ (n) where (n) is based on the position from the right.
ret += 1 << ni.length - i - 1;
}
}
return ret;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
String t = Integer.toBinaryString(i);
System.out.printf("%s = %d%n", t, binaryToNumber(t));
}
}
this is my implementation for the problem
public static void main(String[] args) {
String str = "100101";
System.out.println(toDecimal(str));
}
private static int toDecimal(String binary) {
int result = 0;
for(int i = 0; i < binary.length(); i++) {
int a = (int) binary.charAt(i) - 48;
double secondPart = 1 << (binary.length()-1) - i;
result += a * secondPart;
}
return result;
}
I hope that helps
Salam

Search for an element in array with threads [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have following homework to do:
Implement parallel searching for specified element in array. Use number of threads as a function parameter. Eeach thread checks own array piece size of (ArraySize/NumberOfThreads).
class MyThread extends Thread {
final int[] SEARCH_TAB;
final int RANGE_TAB[][];
final int SEARCH_VALUE;
static int searchIndex = -1;
static boolean isWorking = true;
int whichThread;
MyThread(int[] searchTab, int[][] rangeTab, int searchValue, int whichThread) {
SEARCH_TAB = searchTab;
RANGE_TAB = rangeTab;
SEARCH_VALUE = searchValue;
this.whichThread = whichThread;
}
#Override
public void run() {
for (int i = RANGE_TAB[whichThread][0]; i < RANGE_TAB[whichThread][1] && isWorking; ++i) {
synchronized(this) {
if (SEARCH_TAB[i] == SEARCH_VALUE) {
isWorking = false;
searchIndex = i;
}
}
}
}
}
class Main {
private static int[][] range(int n, int p) {
int[] quantities = new int[p];
int remainder = n % p;
int quotient = n/p;
int i;
for (i = 0; i < p; ++i) quantities[i] = quotient;
i = 0;
while (remainder != 0) {
--remainder;
++quantities[i];
++i;
}
int[][] tab = new int[p][2];
tab[0][0] = 0;
tab[0][1] = quantities[0];
for (i = 1; i < p; ++i) {
tab[i][0] = tab[i-1][1];
tab[i][1] = tab[i][0] + quantities[i];
}
return tab;
}
private static int search(int[] searchTab, int numberOfThreads, int searchValue) {
int[][] rangeTab = range(searchTab.length, numberOfThreads);
Thread[] threads = new Thread[numberOfThreads];
for ( int i = 0; i < numberOfThreads; ++i) threads[i] = new MyThread(searchTab, rangeTab, searchValue, i);
for ( int i = 0; i < numberOfThreads; ++i) threads[i].start();
return MyThread.searchIndex;
}
public static void main(String[] args) {
int[] tab = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10};
int value = 5;
int valueIndex = search(tab, 1, value);
if (valueIndex == -1) System.out.println("Not found.");
else System.out.println(valueIndex);
}
}
This code generally works but cant't find index when one thread is implemented. By the way my teacher said that my code is too long and complicated any suggestions with that?
I will be grateful for any kind of help.
How about the following code:
public class Searcher implements Runnable {
private int intToFind;
private int startIndex;
private int endIndex;
private int[] arrayToSearchIn;
public Searcher(int x, int s, int e, int[] a) {
intToFind = x;
startIndex = s;
endIndex = e;
arrayToSearchIn = a;
}
public void run() {
for (int i = startIndex; i <= endIndex; i++) {
if (arrayToSearchIn[i] == intToFind) System.out.println("Found x at index: " + i);
}
}
}
public class Starter {
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int numberOfThreads = 5;
int x = 20;
findElement(numberOfThreads, x, a);
}
private static void findElement(int numberOfThreads, int x, int[] a) {
int sizeOfa = a.length;
int range = sizeOfa/numberOfThreads;
for (int i = 0; i <= numberOfThreads-1; i++) {
Thread searcher;
if (i == numberOfThreads-1) {
searcher = new Thread(new Searcher(x, i*range, sizeOfa-1, a));
} else {
searcher = new Thread(new Searcher(x, i*range, i*range+range-1, a));
}
searcher.start();
}
}
}
You can still optimize the code e.g. by splitting the rest of the array on all threads instead of just pushing it into the last one (like in my code) but the idea is still the same.
EDIT: I think that there is a problem with your code. It will only show one appearance of x in the array. If you are looking for x = 5 in [5,5,5,5,5] using five threads you can neven know which index will be returned because it depends on how your threads are scheduled. The outcome will be between 0 and 5.

Categories