How to fix a simple (hopefully) error?

I am doing a leetcode solution, and I have this so far:
public class Solution {
public int HeightChecker(int[] heights) {
int[] correctOrder = [];
int amountWrong = 0;
foreach(int i in heights) {
int iter = i;
while (iter <= heights.Length){
iter++;
if (heights[i] < heights[iter]) {
int curr = heights[iter-1];
correctOrder[i] = correctOrder[heights[iter-1]];
heights[iter] = curr;
}
}
}
foreach(int i in heights) {
if(heights[i] != correctOrder[i]){
amountWrong++;
}
}
return amountWrong;
}
}
public class Solution {
public int HeightChecker(int[] heights) {
int[] correctOrder = [];
int amountWrong = 0;
foreach(int i in heights) {
int iter = i;
while (iter <= heights.Length){
iter++;
if (heights[i] < heights[iter]) {
int curr = heights[iter-1];
correctOrder[i] = correctOrder[heights[iter-1]];
heights[iter] = curr;
}
}
}
foreach(int i in heights) {
if(heights[i] != correctOrder[i]){
amountWrong++;
}
}
return amountWrong;
}
}
I am getting this error
No description
37 Replies
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
I'm a beginner so if anything here is bad practice please let me know
Jimmacle
Jimmacle6mo ago
index out of range means you tried to access an array element that doesn't exist valid array indexes are from 0 to 1 less than the length of the array also, arrays are fixed size so if you're trying to put elements into an empty array it won't work
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
so how should i do it instead
Jimmacle
Jimmacle6mo ago
make the array large enough to hold the data you want to put in it, or use a List which automatically expands to add items
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
so would making correctOrder a list fix that error?
Jimmacle
Jimmacle6mo ago
if you change to a list and call Add, that would be one solution or just make the array big enough to hold all the heights like var correctOrder = new int[heights.Length];
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
I switched it to a list looks like a similar if not the same error
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
No description
Jimmacle
Jimmacle6mo ago
and call Add you have to do that to put new items in a list
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
can add put it in a specific spot
Jimmacle
Jimmacle6mo ago
you could use Insert for that
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
Ill try that real quick' Still the same error
public class Solution {
public int HeightChecker(int[] heights) {
List<int> correctOrder = new List<int>();
int amountWrong = 0;
foreach(int i in heights) {
int iter = i;
while (iter <= heights.Length){
iter++;
if (heights[i] < heights[iter]) {
int curr = heights[iter-1];
correctOrder.Insert(iter-1, correctOrder[heights[iter-1]]);
correctOrder.Insert(iter, curr);
}
}
}
foreach(int i in heights) {
if(heights[i] != correctOrder[i]){
amountWrong++;
}
}
return amountWrong;
}
}
public class Solution {
public int HeightChecker(int[] heights) {
List<int> correctOrder = new List<int>();
int amountWrong = 0;
foreach(int i in heights) {
int iter = i;
while (iter <= heights.Length){
iter++;
if (heights[i] < heights[iter]) {
int curr = heights[iter-1];
correctOrder.Insert(iter-1, correctOrder[heights[iter-1]]);
correctOrder.Insert(iter, curr);
}
}
}
foreach(int i in heights) {
if(heights[i] != correctOrder[i]){
amountWrong++;
}
}
return amountWrong;
}
}
Jimmacle
Jimmacle6mo ago
correctOrder[heights[iter-1]] there will not be anything here, the list is empty
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
should i just make correctOrder the same as heights but in list form in the beginning is that possible
Jimmacle
Jimmacle6mo ago
if your "correct order" is a known fixed size there's no reason to use the list
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
then why'd you tell me to
MODiX
MODiX6mo ago
Jimmacle
or just make the array big enough to hold all the heights like var correctOrder = new int[heights.Length];
Quoted by
React with ❌ to remove this embed.
Jimmacle
Jimmacle6mo ago
i'm giving you multiple options, not the answer
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
which option is the best then
Jimmacle
Jimmacle6mo ago
that's up to you, i have no idea what the actual problem being solved is i'm just going based off of a code snippet lists are generally for collections that change size, arrays can be used for collections that are always the same size
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
so array would make more sense but i dont understand where i am getting a negative index
Jimmacle
Jimmacle6mo ago
foreach(int i in heights) seems off this will give you the values in heights, not the indexes are you sure you don't want a for loop?
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
i need all the values, then i am sorting them smallest to biggest then comparing the new sorted array to the old one to see how many are wrong should i use a for loop?
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
public class Solution {
public int HeightChecker(int[] heights) {
int[] correctOrder= new int[heights.Length];
int amountWrong = 0;
foreach(int i in heights) {
int iter = i;
while (iter <= heights.Length){
iter++;
if (heights[i] < heights[iter]) {
int curr = heights[iter-1];
correctOrder[iter-1] = heights[iter-1];
correctOrder[iter] = curr;
}
}
}
foreach(int i in heights) {
if(heights[i] != correctOrder[i]){
amountWrong++;
}
}
return amountWrong;
}
}
public class Solution {
public int HeightChecker(int[] heights) {
int[] correctOrder= new int[heights.Length];
int amountWrong = 0;
foreach(int i in heights) {
int iter = i;
while (iter <= heights.Length){
iter++;
if (heights[i] < heights[iter]) {
int curr = heights[iter-1];
correctOrder[iter-1] = heights[iter-1];
correctOrder[iter] = curr;
}
}
}
foreach(int i in heights) {
if(heights[i] != correctOrder[i]){
amountWrong++;
}
}
return amountWrong;
}
}
updated code, similar error
No description
Jimmacle
Jimmacle6mo ago
if you need the index, use a for loop your code doesn't make sense because you're using the actual height value as an index into the array
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
i need the values, no?
Jimmacle
Jimmacle6mo ago
i is not storing an index in your code, it's storing a height then you're trying to use it as an index into the arrays, which is probably where your bad index is coming from
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
switched it and im still getting that same error ^
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
No description
Jimmacle
Jimmacle6mo ago
i <= heights.Length remember what i said about valid indexes heights.Length would be 1 past the end of the array
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
okay that makes sense so heights.Length-1 then
Jimmacle
Jimmacle6mo ago
or just i < heights.Length
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
same error still
Jimmacle
Jimmacle6mo ago
then you have more places where you're making mistakes can you run this code in an actual IDE with a debugger? it will tell you exactly which line is causing the exception
samisaskinnyqueen
samisaskinnyqueenOP6mo ago
yeah give me a second
blueberriesiftheywerecats
You have 2 of that checks, did you change both? And it reminds me of bubble sort, isnt it? Can you show full code?
Want results from more Discord servers?
Add your server