Cześć, mam problem ze zrozumieniem w jaki sposób wyświetlane są tablice w Java. Mianowicie o co chodzi z takimi liniami w kodzie:
liczby[i] = i + 1;
if(nums[b-1] > nums[b]) {
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
to tylko przykłady ale widzę je praktycznie ciągle wyglądają dziwnie wręcz idiotycznie. Na dole daję kod i link z którego wziąłem te linijki
Hi, I'm having trouble understanding how arrays are displayed in Java. More specifically what exactly are these lines about?
liczby[i] = i + 1;
if(nums[b-1] > nums[b]) {
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
These are for examples but I see them very often. At the bottom I paste code and link where I get those examples from
https://codegym.cc/quests/lectures/pl.questsyntax.level07.lecture03
public class HelpSystem2 {
public static void main(String [] args){
int nums[] = {99,-10,100123,18,-978,5623,463,-9,287,49};
int a,b,t;
int size;
size=10;
System.out.println("Pierowtna tablica: ");
for(int i=0; i<size; i++)
System.out.println(" " +nums[i]);
System.out.println();
for(a=1; a<size; a++)
for(b=size-1; b>=a; b--) {
if(nums[b-1] > nums[b]) {
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
System.out.println("Posortowana tablica:");
for(int i=0; i<size; i++)
System.out.println(" " +nums[i]);
System.out.println();
}
}
Problem z tablicami/Problem with arrays
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
4 September 2023, 17:06
This is not really a good example to learn about arrays. This is code to look at if you are already familiar with arrays and want to learn about sorting algorithms.
An array introduction you can eg. find on the Oracle site
+1
Anonymous #11274698
5 September 2023, 10:54
Thank you for answer!
0
Anonymous #11274698
5 September 2023, 12:50
But in this task to complete it you can/have to use this loop. This task is about ArrayList but still
for(int i=0; i<list.size();i++){
int z = list.size() - i - 1;
System.out.println(list.get(z));
https://codegym.cc/tasks/pl.codegym.task.task07.task0714
0
Thomas
5 September 2023, 13:21
'You can use this loop' is correct. But was that the question?
- Do you have problems understanding arrays or
- do you have problems with collections or
- don't you understand how to use loops to access/ iterate over containers.
0
Anonymous #11274698
5 September 2023, 13:58
Okay Im chaotic sometimes. I dont get those lines :
int z = list.size() - i - 1;
if(nums[b-1] > nums[b]) {
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
Why theres i - 1 and b-1?
Any for what someone make those t = nums etc.
0
Thomas
5 September 2023, 14:45
Ah, back at the sorting algorithm again.
But one by one.
Arrays are some sort of container. You define its length and then you're able to put elements of the defined type in it.
eg. an int array with 5 elements.
That'll create an array with the 5 elements and you can access these elements using indexes from 0 to 4. The last element isn't 5 or intArray.length but intArray.length - 1 (keep that in mind).
To print the value of the elements there are various possibilities. One is a regular loop.
This is what you usally use. A loop starting from 0 to < array.length
Let's fill the array with some content (numbers from 1 to 5)
And now let's print the array in reverse order. You now could start the loop from length -1 and end it when the counter variable reaches zero, like
or you stay with the loop as shown in the first examples (and that's what I usually do because you see at the first view that you loop over an entire array). The reversing you do when accessing the element. Eg. the counter starts at 0, then you access the last element using intArray[intArray.length - i - 1]
intArray.length is 5, i is 0 so that's 5 - 0 - 1 = 4, the last element. When i is 1, then it accesses the element with the index 3 (second last)
OK, no more space. I wrote the code here so it may contain errors. The other code is for swapping ele +1
Anonymous #11274698
22 September 2023, 11:36
It definitly helps thank you!
0