What's new

Closed Array. how to store same value as 1

Status
Not open for further replies.

Princess Serrano

Eternal Poster
Joined
Apr 2, 2016
Posts
429
Reaction
285
Points
285
Tanong lang po guys.

In array for example i have


int [] val = { 1, 2, 1 }

I will re store them in val as
val = { 1, 2 }


Possible po ba yun? If possible. Ano na value nung pangatlong set sa array? Specifically yung val [2] ??

Patulong naman po salamat.
 
I think ang array hindi puwedeng palitan ng values at runtime.

Meaning pag na-declare mo na with values, hindi mo na puwedeng baguhin sa runtime.

If you try to declare and assign a value at runtime, error kaagad.
 
store lng sa new array variable paps .. kung di pwede :) di ko kc natry .. puro imagination lng ^^

I think ang array hindi puwedeng palitan ng values at runtime.

Meaning pag na-declare mo na with values, hindi mo na puwedeng baguhin sa runtime.

If you try to declare and assign a value at runtime, error kaagad.
 
Nasubukan mo ba i-test?
Btw to re-assign an array, dapat may new int[]. First line will work, but the second will not.

Arrays in Java are objects and therefore can be changed/re-assigned in runtime (unless declared final)
Hindi siya katulad ng stack-allocated arrays ng C and C++ na fixed ang dimensions. They're similar to dynamically-allocated pointers na kailangan i-re-allocate kung gusto mong palitan ang size.

Feel free to do this
Code:
int[] val = {1,2,1};
val = new int[]{1, 2};

Kung gusto mo lang i-slice yung Array, you can try this
Code:
int[] val = {1,2,1};
// will produce the same result
val = java.util.Arrays.copyOfRange(val, 0, 2);

Spoiler contents are visible only to Established Members.
 
Last edited:
Salamat po sa mga sumagot. Gusto ko lang po sana na gawing isa nalang yung mga magkakaparehas sa array. For example nag input yung user ng dalawang 5 at dalawang 3. Automatic, 3 and 5 nalang yung nasa loob ng array.
 
ah, gusto mo unique ang laman ng array?
gamitin mo ang Set and HashSet
They are part of the Java Collection Framework

ganito ang paggamit:

---code starts----
//declare and you will need java.util.*

Set<Integer> setOfIntegers = new HashSet<Integer>();

//add the values (subukan mo duplicate values para malaman mong unique nga

setOfIntegers.add(5);
setOfIntegers.add(3);
setOfIntegers.add(12);

//just print them out to confirm

for(Integer anInteger:setOfIntegers){
System.out.println(anInteger);
}
---code ends---


Side note para sa iyo, basahin mo ang Java Documentation about JAVA COLLECTION FRAMEWORK. Merong few classes like Set, HashSet, ArrayList, etc. Magugustuhan mo at these are very very useful classes.
 
ah, gusto mo unique ang laman ng array?
gamitin mo ang Set and HashSet
They are part of the Java Collection Framework

ganito ang paggamit:

---code starts----
//declare and you will need java.util.*

Set<Integer> setOfIntegers = new HashSet<Integer>();

//add the values (subukan mo duplicate values para malaman mong unique nga

setOfIntegers.add(5);
setOfIntegers.add(3);
setOfIntegers.add(12);

//just print them out to confirm

for(Integer anInteger:setOfIntegers){
System.out.println(anInteger);
}
---code ends---


Side note para sa iyo, basahin mo ang Java Documentation about JAVA COLLECTION FRAMEWORK. Merong few classes like Set, HashSet, ArrayList, etc. Magugustuhan mo at these are very very useful classes.
Maraming salamat po kuya! :)
Medyo nalilito lang po ako haha. Pero try ko din po magtanong senyo. Array palang kami e
 
Princess Serrano
Try your very very best... para hindi ikaw yung student na....."Guys, pahingi naman ng program for my thesis dyan o. Pasahan na namin sa next week."....pagdating ng araw...hahahaha.

Kung nalilito ka, i-drawing mo....visualize....bigkasin mo sa tagalog. Sometimes it helps.

At saka huwag mong hintayin yung teacher mo na ituro lahat... unahan mo na kahit hindi pa scheduled na pag aralan.
:)
 
Status
Not open for further replies.
Back
Top