I'm trying to do Sort method, however i get this error:
IndexOutOfRangeException, on the line if(chars[i] > chars1[y]). Amount is equal to 25
string temp1;
for (int i = 0; i < amount; i++)
{
for (int y = i + 1; y < amount - 1; y++)
{
var chars = Duomenys[i].Pozicija.ToCharArray();
var chars1 = Duomenys[y].Pozicija.ToCharArray();
if (chars[i] > chars1[y])
{............}
You are using the same indices (i and y) to identify locations within the array Duomenys and chars/chars1, which seem like very different things. Can't tell what you should be doing instead, given the lack of information provided.
Related
so if you input 3x3 array it should be like this
987
654
321
and i want to bubble sort it to
123
456
789
but i have problem on end line
can anyone help me
When I execute my code, the output looks like this:
Please enter number of rows:2
Please enter number of columns: 2
Please enter 4 numbers to sort: 8 4 6 2
Sorted numbers:
[[I#776ec8df [[I#776ec8df [[I#776ec8df [[I#776ec8df
import java.util.Scanner;
public class 2dsort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter number of rows: ");
var row = sc.nextInt();
System.out.print("Please enter number of columns: ");
var col = sc.nextInt();
int[][] matrix = new int[row][col];
System.out.print("Please enter " + (row*col) + " numbers to sort: ");
for(int x = 0; x < row; x++)
for(int y = 0; y < col; y++)
{
matrix[x][y] = sc.nextInt();
}
for(int x = 0; x < matrix.length; x++)
for (int y = 0; y < matrix[x].length; y++)
for(int t = 0; t < matrix[x].length - y - 1; y++)
if(matrix[x][t] > matrix[x][t+1])
{
int temp = matrix[x][t];
matrix[x][t] = matrix[x][t+1];
matrix[x][t + 1] = temp;
}
//I think this is my problem but idk how to fix
System.out.println("Sorted numbers:");
for(int x = 0; x < matrix.length; x++)
for (int y = 0; y < matrix[x].length; y++)
System.out.print(matrix + " ");
System.out.println();
}
}
Your issue appears to be here:
System.out.print(matrix + " ");
You're not printing out the values held by the matrix.
You likely want to retrieve the values at the specific indices using matrix[x][y]
I ran your code using System.out.print(matrix[x][y] + " "); and while the code execution worked and printed the matrix values appropriately, your sorting algorithm isn't giving you the results you want. I'll leave that for you to fix, but your printing issue is solved.
Can I get some help with this project I am trying to make a 9x9 of ellipsis in javafx. I have the code for the ellipsis but I can't figure out the logic to make the following happen.
Example of what I am looking for
I have used the regular for loop that just gives me 9x9. ellipsis stacked on top of one another
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
so what do I have to do to this for loop, to make it look like the picture above
any help would be great thanks.
Just determine the horizontal/vertical offset of the circles.
Every other row contains one more circle and starts half of the offset further to the left than the smaller rows.
final double fieldWidth = 50;
final double fieldHeight = 50;
final double radius = 20;
Pane parent = new Pane();
boolean bigRow = true;
final int rows = 9;
final int columns = 9;
for (int y = 0; y < rows; y++, bigRow = !bigRow) {
double offsetX = bigRow ? fieldWidth / 2 : fieldWidth;
double offsetY = fieldHeight * y + fieldHeight / 2;
for (int x = 0, size = bigRow ? columns : columns - 1; x < size; x++) {
Circle c = new Circle(offsetX + x * fieldWidth, offsetY, radius, Color.TRANSPARENT);
c.setStroke(Color.BLACK);
parent.getChildren().add(c);
}
}
I'm now using NPOI to cope with Excel export, and here's my codes (part in .NET):
int rowIndex = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < cellCount; j++)
{
cell = dataRow.CreateCell(j,CellType.String);
cell.SetCellValue(new HSSFRichTextString(dt.Rows[i][j].ToString()));
}
rowIndex++;
}
What makes me feel surprised is there's a list whose number string is "20150525", and it will be analyzed as "2015……E+10" formation (scientific number formation). However I wanna keep it as a string value. So How?
Thanks!
In fact we have to set a CellStyle, snippet of sample codes is below:
IRow row = book[0].CreateRow(rowIndex + 1);
ICell rowCell = null;
rowCell = row.CreateCell(colIndex);
rowCell.SetCellValue(realCellValue);
ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#");
rowCell.CellStyle = cellStyle;
I need to loop through an array using the for clause, but starting at some specific index and just to a maximum of iterations.
The code below does the task, but it looks awful to me: it's there a better way?
var offset = 10, max = 5;
for (var i = 0; (i + offset) < data.length && i < max; i++) {
doSomething(data[i + offset]);
}
If I am understanding your question correctly you would just need to initialize i to the offset.
var offset = 10, max = 5 + offset;
for (var i = offset; i < data.length && i < max; i++) {
doSomething(data[i]);
}
edit: didn't understand the max at first.
I have a 16 MB text document containing a single huge string of letters and numbers without any separators. Excerpt: "as81jsa8sm1o1kmka9s93m1l"
Is there a simple way to alphabetize all of the characters, without having to write a script? I'm afraid JS will crash under the weight of the file.
Thanks.
If you know the string only contains letters and numbers, you can use a bucket sort and achieve good performance.
I am not sure what language you are using, so I'll assume you can read the string character by character. my solution is psuedocode
int[] buckets = int[36]; // 26 for letters, 10 for numbers; assuming only lowercase characters
while(string.hasNext()) {
char x = next character in string;
if(x.isAlpha()) {
buckets[x-'a']++;
}else {
buckets[26 + x - '0']++
}
}
To print out the sorted string:
string s = ""; // at the end of the loops, s will contain the sorted string
for(int i =0 ; i < 26; ++i) {
int y = buckets[i];
for(int j = 0; j < y; ++j) {
s+=(y+'a');
}
}
for(int i =0 ; i < 10; ++i) {
int y = buckets[i+26];
for(int j = 0; j < y; ++j) {
s+=(y+'0');
}
}