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
samisaskinnyqueenOP7mo ago
I'm a beginner so if anything here is bad practice please let me know
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo ago
so how should i do it instead
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo ago
so would making correctOrder a list fix that error?
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo ago
I switched it to a list looks like a similar if not the same error
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
No description
Jimmacle
Jimmacle7mo ago
and call Add you have to do that to put new items in a list
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
can add put it in a specific spot
Jimmacle
Jimmacle7mo ago
you could use Insert for that
samisaskinnyqueen
samisaskinnyqueenOP7mo 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
Jimmacle7mo ago
correctOrder[heights[iter-1]] there will not be anything here, the list is empty
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
should i just make correctOrder the same as heights but in list form in the beginning is that possible
Jimmacle
Jimmacle7mo ago
if your "correct order" is a known fixed size there's no reason to use the list
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
then why'd you tell me to
MODiX
MODiX7mo 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
Jimmacle7mo ago
i'm giving you multiple options, not the answer
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
which option is the best then
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo ago
so array would make more sense but i dont understand where i am getting a negative index
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo 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
samisaskinnyqueenOP7mo 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
Jimmacle7mo 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
samisaskinnyqueenOP7mo ago
i need the values, no?
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo ago
switched it and im still getting that same error ^
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
No description
Jimmacle
Jimmacle7mo ago
i <= heights.Length remember what i said about valid indexes heights.Length would be 1 past the end of the array
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
okay that makes sense so heights.Length-1 then
Jimmacle
Jimmacle7mo ago
or just i < heights.Length
samisaskinnyqueen
samisaskinnyqueenOP7mo ago
same error still
Jimmacle
Jimmacle7mo 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
samisaskinnyqueenOP7mo 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